Import CSV file in SQL keeping leading zero

Dmtr_Grms 331 Reputation points
2023-05-22T19:12:21.7566667+00:00

Good evening, I want to import a CSV file in SQL using Bulk import. I have a field containing the following value 010010 and that field has to remain like that in database. I have done several test but all of them have for result 10010 ---> leading zero is missing. Could someone explain what I should do to avoid that type of problem? Thanks

As requested by @Yitzhak Khabinsky

FROM 'C:\Users\xxxxx\Downloads\Sviluppo C#\SQL_Dati per tests\ScenariosUploadCSV.csv' WITH ( CODEPAGE = '65001', FORMAT = 'CSV', FIELDQUOTE = '', FIRSTROW = 2, FIELDTERMINATOR = ';', ROWTERMINATOR = '\n', TABLOCK )

GO

Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,480 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,772 questions
{count} votes

Accepted answer
  1. Erland Sommarskog 101.4K Reputation points MVP
    2023-05-22T21:59:22.73+00:00

    Sounds like the data type of the column is int. You will need a string data type for the leading zero to be maintained.

    Else, to assist with BULK INSERT questions, it helps a lot if you include:

    1. The CREATE TABLE statement for your target table.
    2. You attach a sample file that demonstrates the problem. (We need to see the exact bytes, so including it as text in the post is not a good idea.)
    3. Any format file you are using.
    4. The complete statement as you are running it right now.
    0 comments No comments

0 additional answers

Sort by: Most helpful