Skip to content
Advertisement

Is my understanding of how FIELDQUOTE works correct?

I am using Azure Synapse and I m querying the following CSV file:

Here is the T-SQL query:

Here is the output:

I then altered the CSV file to add a colon just before the letter G as so:

I get:

This lead me to make the following conclusions about FIELDQUOTE:

  • The query as a whole scans each row, character by character looking for either a FIELDQUOTE or a FIELDTERMINATOR. If it finds a FIELDQUOTE first then it ignores any FIELDTERMINATORS until it finds another FIELDQUOTE (I’m basing this on the last row).
  • The FIELDQUOTE should have a FIELDTERMINATOR immediately before it in order for the FIELDQUOTE to work as expected (I’m basing this on the first row of the second example).

I could be well off the mark here but can anyone give me some feedback on this?.

When I tried running the query above on this CSV:

It returned the following:

So I tried with this CSV:

However I got the following error:

I then tried the CSV:

And I got the following:

Lastly, I tried this CSV:

I got the following error:

The strangest one was when I tried running:

I got:

Advertisement

Answer

  1. “The query as a whole scans each row, character by character looking for either a FIELDQUOTE or a FIELDTERMINATOR. If it finds a FIELDQUOTE first then it ignores any FIELDTERMINATORS until it finds another FIELDQUOTE (I’m basing this on the last row).”

    From my comment: I believe your first conclusion is accurate. Specifying the character for string encapsulation or FIELDQUOTE in this dialect of csv parser, acts exactly as you describe. It allows row delimiters/terminators, field delimiters/terminators, and even the character used for string encapsulation itself (in most parsers when specifying an escape character) to exist in the string literal for that field.

    Oddly, I don’t think that OPENROWSET() allows for specifying an escape character, so I’m not sure how you would get a double quote character into your field/string-literal if it’s encapsulated. But that’s not part of your question just an observation reading through the documentation. I would be curious what OPENROWSET() would do with 1,"A"":",G or 1,"A":",G

  2. “The FIELDQUOTE should have a FIELDTERMINATOR immediately before it in order for the FIELDQUOTE to work as expected (I’m basing this on the first row of the second example).”

    Yes. I believe this is true as well and is true with any CSV parser I’ve ever used. The string encapsulation character must follow either row or field terminator/delimiter and must be followed by the row or field terminator/delimiter for it to be recognized as an encapsulated string (or not error). I would be curious what OPENROWSET() would do with 1,"A":,G.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement