SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server

This is a 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 TestData
GO
CREATE TABLE CSVTest
(ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME)
GO

Create CSV file in drive C: with name sweetest. text with the following content. The location of the file is C:\csvtest.txt

1,James,Smith,19750101

2,Meggie,Smith,19790122

3,Robert,Smith,20071101

4,Alex,Smith,20040202

SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server csv1

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
CSVTest
FROM 'c:\csvtest.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
--Check the content of the table.
SELECT *
FROM CSVTest
GO
--Drop the table to clean up database.
DROP TABLE CSVTest
GO

SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server csv2

Reference : Pinal Dave (https://blog.sqlauthority.com)

CSV, SQL Scripts, SQL Utility
Previous Post
SQL SERVER – Simple Example of WHILE Loop with BREAK and CONTINUE
Next Post
SQL SERVER – Sharpen Your Basic SQL Server Skills – Database backup demystified

Related Posts

839 Comments. Leave new

  • Imran Mohammed
    February 4, 2009 9:06 am

    @Satrebla

    If you have( ” ) character at fixed length for every value that goes into that column, then you can use substring function and select only those characters that you want ignoring rest of them and concatinate with other values (if needed)

    Regards,
    IM.

    Reply
  • @Imran Mohammed

    Thanks for reply, but lenth is variable. For example:

    “abc,qwe,zxc”,123,txt
    “qwe,asd,fgh,jkl”,456,qqq
    “12233,456789”,rty,159

    ..?

    Reply
  • replace commas that aren’t embedded between quotes with a character that won’t be used. change fieldterminator to be that new character.

    can probably also use fmt file, but i’ve never used one so not positive

    Reply
  • Hi,

    I would like to know how do we import a remote text to some other server in sql server 2005 ?

    Reply
  • how or where do you write the script that you are publishing. I am new to the import but not asp.net. How do you import into a table that you have already constructed with the gui in asp.net.

    Reply
  • Thanks a lot..
    It is working fine

    Reply
  • Praveen Agrawal
    March 16, 2009 3:23 pm

    How We can Insert The Data in sq l server through Excel Sheet. Here i don’t have ‘,’ field or Line termination.

    Reply
  • select *
    from
    OPENROWSET(‘Microsoft.Jet.OLEDB.4.0’,
    ‘Excel 8.0;Database=c:.xls’, [$])
    ) AS x

    Reply
  • Hi Praveen,

    select *
    from
    OPENROWSET(‘Microsoft.Jet.OLEDB.4.0’,
    ‘Excel 8.0;Database=c:ExcelFileName.xls’, [YourSheetName$])
    ) AS x

    Thanks,

    Reply
  • Praveen Agrawal
    March 17, 2009 9:24 am

    Thanks Tejas

    Reply
  • Praveen Agrawal
    March 17, 2009 12:14 pm

    Hi All,

    how to join two different tables on different server

    Reply
  • Hi Praveen,

    Is SQL PORT open for any server?

    If yes, then you can use:

    SELECT *
    FROM table A
    INNER JOIN
    (
    OPENROWSET(‘SQLOLEDB’,’ServerAddress’;’User’;’Password’,
    ‘select * from
    table
    ‘)
    ) B
    ON A.id = b.Id

    Thanks,

    Tejas

    Reply
  • Hi,

    I had implemented this “Bulk Insert” in Sql 2005, i am getting

    Msg 102, Level 15, State 1, Line 7
    Incorrect syntax near ‘‘’.

    my Statement ..

    BULK

    INSERT ccprocessorstandardpayee
    FROM ‘c:\csvtest.txt’
    WITH
    (
    FIELDTERMINATOR = ‘,’,
    ROWTERMINATOR = ‘\n’
    )
    GO

    please help me..

    thnk u..

    Reply
  • Just a note that somehow – doesn’t matter to me – I was stymied at first by “fancy” quotes or apostrophes. That is, I was inclined to copy/paste your stuff, and SQL returned syntax errors having to do with the use of NON- straight (up & down) single quotes. I happened to figure out the problem but someone else might get frustrated prematurely. Since there’s some problem having to do with Full Text searching that prevented me from importing those big (and no doubt beautiful) sample DB’s that MS makes available, this post (YOURS) is/was EXTREMELY HELPFUL. Thanks!!

    Reply
  • Dmitriy Zasyatkin
    March 24, 2009 1:39 am

    Thank you very much! This was very helpful.

    Reply
  • Hi,

    I’m trying to upload a file to my database and it wont work at all, I get this message:

    #1064 – You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘BULK INSERT tblPostcodes FROM ‘c:postcodes.txt’ WITH ( FIELDTERMINATOR’ at line 1

    when I run:

    BULK
    INSERT tblPostcodes
    FROM ‘c:postcodes.txt’
    WITH
    (
    FIELDTERMINATOR = ‘,’,
    ROWTERMINATOR = ‘n’
    )
    GO

    Any ideas?

    Reply
  • well i try to upload the csv file throgh the below given query

    BULK
    INSERT CSVTest
    FROM ‘c:\CSVTEST.txt’
    WITH
    (
    FIELDTERMINATOR = ‘|’,
    ROWTERMINATOR = ‘\n’
    )
    GO

    but it is showing error message when i ran this above query
    the error message is given below:

    Msg 102, Level 15, State 1, Line 3
    Incorrect syntax near ‘‘’.
    Msg 319, Level 15, State 1, Line 4
    Incorrect syntax near the keyword ‘with’. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

    Reply
  • well i try to upload the csv file throgh the below given query

    BULK
    INSERT CSVTest
    FROM ‘c:\CSVTEST.txt’
    WITH
    (
    FIELDTERMINATOR = ‘|’,
    ROWTERMINATOR = ‘\n’
    )
    GO

    but it is showing error message when i ran this above query
    the error message is given below:

    Msg 102, Level 15, State 1, Line 3
    Incorrect syntax near ‘‘’.
    Msg 319, Level 15, State 1, Line 4
    Incorrect syntax near the keyword ‘with’. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.

    please tell me why this is happening and pls give me the solution for this

    Reply
  • ur Bulk insert works as following way: in LAN only

    Bulk Insert cv_Test from ‘\\Rahool\SharedDocs\Book2.txt’ with
    (
    FIELDTERMINATOR= ‘,’,

    ROWTERMINATOR= ‘\n’
    )
    Go

    Reply
  • Thanks this is great.

    You are a true guru at sql. This has solved a massive problem.
    I was trying to insert a csv into sql express 2005.
    Which is witout the import functions. this code solved that problem.

    Thanks a lots!!!

    Reply

Leave a Reply