February 6, 2008 by pinaldave
This is very common request recently-how to import CSV file into SQL server? How to load CSV file into SQL Server database table? How to load comma delimited file into SQL server? Let us see the solution in quick steps.
CSV stands for comma separated values, sometimes also called comma delimited values.
Create testtable
Use test1_ocreate table csvtest (ID int, firstname varchar (40), lastname varchar (40), birthdate smalldatetime) Go
Create CSV file in drive C: With name csvtest.txt with following content. The location of the file is c: \ csvtest.txt
1, James, Smith, 197501012, Meggie, Smith, 197901223, Robert, Smith, 200711014, Alex, Smith, 20040202
Now run following script to load all the data from CSV to database table. If there is any error in any row it will be not inserted but other rows will be inserted.
Bulk insert csvtestfrom 'C: \ csvtest.txt 'with (fieldterminator =', ', rowterminator =' \ n') Go
Check the content of the table.
Select * From csvtestgo
Drop the table to clean up database.
Drop table csvtestgo