Cleaning SQL Columns for Export

Several times a year I’m asked to run some queries and export the results to Excel, CSV or Tab delimited files.

If you’ve ever done this you may have run into bad exports due to line ending or tab characters in the DB columns. I’m always forgetting how to clean these up so here’s a reminder to me.

REPLACE( REPLACE( REPLACE( CAST( <column> AS VARCHAR(MAX) ), CHAR(13), ' ' ), CHAR(10), ' ' ), CHAR(9), '' ) AS '<column>'

Leave a Reply

Your email address will not be published. Required fields are marked *