Monday, March 16, 2015

Escaping Text for CSV in TSQL

Needed string fields output in CSV, but with punctuation being used, that can be crazy. Figured it out, at least for the cases I'm looking at.


SELECT
cr.Id
,'"' + REPLACE(cr.Description, '"', '""') + '"' AS Descrption
,'"' + REPLACE(rr.Response, '"', '""') + '"' AS Response
,'"' + REPLACE(rr.Reason, '"', '""') + '"' AS Reason
,cr.ResolvedDate
FROM Reviews cr
LEFT OUTER JOIN Responses rr ON cr.Id = rr.ReviewId
ORDER BY cr.Id, rr.Id DESC

No comments:

Post a Comment