Skip to main content
November 23, 2012
Question

Escaping Quotes while Creating .csv files

  • November 23, 2012
  • 1 reply
  • 6755 views

Hi,

I'm using the following query/<cffifle> (see below) command to create a .csv with information from our SQL 2008 database. However, I'm having a problem when exporting data fields with certain characters in them. Specifically, line breaks and ' " '

For example, one of our product's description may be:

18.5"  HP LED BL Monitor

The " character disrupts the format of the field, because field-transitions are marked with double quotes.

Is there a way to escape this character? (And perhaps others, such as line breaks and special characters)

Alternatively, is there a better way to export specific inventory information other than the current method?

Notes :

This is a verbose function that runs in a web browser. This detail isn't necessary.

I can provide the full .cfm file as well as the ouput if necessary.

-------------------------------------------------------------------------------------------------------------------------------

<cfoutput>Getting Active Products..</cfoutput><br>

                    <cfquery name="getActiveProducts" datasource="DATABASE">

                                        USE DATABASE

                                        SELECT dbo.Item.ID, dbo.Item.ItemLookupCode, dbo.Item.Description, dbo.item.ExtendedDescription, PriceA, dbo.Department.Name +'+'+ dbo.category.Name as CatSubCat, dbo.Item.Quantity - dbo.Item.QuantityCommitted as AVAIL

                                        FROM Item

                                        JOIN Department

                                        ON Item.DepartmentID = Department.ID

                                        JOIN Category

                                        ON Item.CategoryID = Category.ID

                                        Where WebItem = 'True'

                                        Order By ItemLookupCode

                    </cfquery>

..

                              <cffile action="write"

                              file="\\server\storage\X-Cart\ExportFile\ExportFile.csv"

                              output="[PRODUCTS],,,,"

                              addnewline="yes">

..                    

                              <cffile action="append"

                              file="\\server\storage\X-Cart\ExportFile\ExportFile.csv"

                              output= "!PRODUCTID,!PRODUCTCODE,!PRODUCT,!DESCR,!PRICE,!CATEGORY,!AVAIL,!FORSALE,!CATEGORYID"

                              addnewline="yes">

..

                    <cfoutput query="getActiveProducts">

                              <cffile action="append"

                              output='"#trim(ID)#","#trim(ItemLookupCode)#","#trim(Description)#","#trim(ExtendedDescription)#","#trim(PriceA)#","#trim(CatSubCat)#","#trim(AVAIL)#","Y",""'

                              file="\\server\storage\X-Cart\ExportFile\ExportFile.csv"

                              addnewline="yes">

                    </cfoutput>

..<br>

<cfoutput>End of Getting Active Products</cfoutput><br>

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The ouput then becomes

"2603","A5V72A8ABA",""18.5"  HP LED BL Monitor"","""","109.0000","Monitor+LED-Backlit","52","Y","" which will not import correctly.

The field should be totally enclosed by separating characters:

[CHAR]18.5"  HP LED BL Monitor[CHAR]

Any help on this matter would be greatly appreciated.


Thank You

This topic has been closed for replies.

1 reply

BKBK
Braniac
November 24, 2012

You could, within terms likely to contain quotes, escape each double quote with a further double quote and each single quote with a further single quote, like this:

<cfoutput query="getActiveProducts">

<cfset description = replace(getActiveProducts.description,'"','""','all')>

<cfset extendedDescription = replace(getActiveProducts.extendedDescription,'"','""','all')>

<cfset catSubCat = replace(getActiveProducts.catSubCat,'"','""','all')>

<cfset description = replace(description,"'","''","all")>

<cfset extendedDescription = replace(extendedDescription,"'","''","all")>

<cfset catSubCat = replace(catSubCat,"'","''","all")>

<cffile action="append" output='"#trim(ID)#","#trim(ItemLookupCode)#","#trim(description)#","#trim(extendedDescription)#","#trim(PriceA)#","#trim(catSubCat)#","#tr im(AVAIL)#","Y",""' file="\\server\storage\X-Cart\ExportFile\ExportFile.csv" addnewline="yes">

</cfoutput>

November 24, 2012

Thanks for your reply. However, this didn't seem to work for me. The output becomes

[PRODUCTS],,,,

!PRODUCTID,!PRODUCTCODE,!PRODUCT,!DESCR,!PRICE,!CATEGORY,!AVAIL,!FORSALE,!CATEGORYID

"2603","A5V72A8ABA","18.5"  HP LED BL Monitor","A:

LineBreak

Test!*","109.0000","Monitor+LED-Backlit","48","Y",""

To further describe my situation, I'm importing the contents of this .csv file into X-Cart (online shopping cart). What is happening is that the " symbol, which needs to be adjacent to certain terms (i.e. 19" monitor 6" Cable), is being displaced.

The previous .csv line would produce a product title of

18.5 HP LED BL Monitor"

When obviously, it needs to be 18.5" HP LED BL Monitor. This is because the PRODUCT column is being imported twice, once for 18.5, and again for HP LED BL Monitor".  The two fields are simply concatenated and applied as the product name.

I have successfully used a semicolon in place of commas, with the only stipulation that a line break will disrupt the import feed, and putting quotes around the field to escape this character yields the very results I'm finding here.

November 24, 2012

Could you please show us the correct (that is, expected) output line for the product with ID 2603.


[PRODUCTS],,,,

!PRODUCTID,!PRODUCTCODE,!PRODUCT,!DESCR,!PRICE,!CATEGORY,!AVAIL,!FORSALE,!CATEGORYID

"2603","A5V72A8ABA","18.5""  HP LED BL Monitor","a:

LineBreak

Test!*","109.0000","Monitor+LED-Backlit","48","Y",""

If I simply put another " symbol between the 5 and the ", then it is correct.