Home > DataBases, MSSQL > MSSQL CSV Select Column

MSSQL CSV Select Column

I always need this and it is really simple but I don’t use it enough to remember how to do it each time. This will take the ID column of the selection and create a CSV field for my use elsewhere:

select substring((
     select ',' + CAST(questionid as VARCHAR) 
          from quiz_questions 
          order by questionid 
          for xml path('')),2,200000) as CSV

Notice I had to CAST the integer key column to a VARCHAR.
Here is the “greater” reference:
http://blog.sqlauthority.com/2009/11/25/sql-server-comma-separated-values-csv-from-table-column/
Did I mention this guy is great?

Categories: DataBases, MSSQL
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment