Monthly Archives: April 2011

Convert CSV data into a SQL table

Recently I have been working with some report data that is stored statically in a database in CSV format. I had a need to have that CSV data displayed in a tabular format. I searched around and couldn’t find anything written to do this in SQL, so I decided to write a stored procedure to do this very thing. It will take a CSV and parse it out into a SQL table. For instance if I have report data that looks like this…

It would be transformed into this …

FIRSTNAME LASTNAME DOB CHILDREN
John Smith 1/5/1980 Bob,Sally,Joe,Chris
Jane Smith 2/25/1982 Bob,Sally,Joe,Chris
Bruce Wayne 5/13/1975
Peter Parker 5/23/1970

Note that in the last column the commas are preserved because they are within the quotation mark text qualifiers. Below is the stored procedure that does this. Feel free to use it, and leave a comment if you found it useful.