Sql server: Bulk insert. Change from comma to dot

Rals 1 Reputation point
2022-12-02T21:08:33.953+00:00

Hi
I need to load a csv file using bulk insert.
In the csv file the numeric fields have comma instead of dot. (So i m seeing it as a varchar not a numeric)
How can i change this during bulk insert.
What should i use in the file format?
Thanks

SQL Server Other
{count} votes

5 answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,586 Reputation points
    2022-12-02T22:02:23.847+00:00

    Hi @Rals ,

    You can try along the following conceptual example.

    SQL

    INSERT INTO dbo.destinationTable (ID, decimalColumn)  
    SELECT ID, TRY_CAST(REPLACE(column1, ',','.') as DECIMAL(10,2))   
    FROM OPENROWSET(  
       BULK 'E:\Temp\input.csv',  
       FORMATFILE = 'e:\Temp\formatFile...'  
    ) AS t;  
    
    0 comments No comments

  2. Rals 1 Reputation point
    2022-12-03T05:37:06.94+00:00

    Hi thanks, but i have 7csv, with around 100 columns each…i need to find something to use it for all


  3. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-12-03T17:54:04.053+00:00

    Unfortunately, I was not very successful. My idea was to use the bcp command-prompt utility, because bcp has the option -R which means that regional settings should be respected.

    But, alas, it does not seem to apply when reading data for numeric/decimal, but bcp wants a decimal point also when I give -R. I was able to load data, when I changed the data type to money. money corresponds to decimal(18,4), and your data seems to have more decimals than just four, so I don't think this is a workable way for you.

    However, there may be an even more fundamental problem. In your sample file above, there are several entries with two commas in them. I don't know if you were sloppy when you composed the sample, of if this reflects the actual data. (When trying to load the file, I assumed the former, and just removed the extraneous commas.) But it could be a good idea that you investigate this first before we continue the conversation.


  4. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-12-03T19:56:29.167+00:00

    The only other option I can see to a replace() for every column, is to massage the files before you load them. That is, replace all commas with period. If you are only doing this once, you could do this a in a text editor. Else you would have to write a short program in the language of your choice for the task.

    One caveat though: if there is string data with commas, those commas will also be changed into periods.

    If you want to do everything inside SQL Server, I am afraid that the original solution suggested by Yithak is your only option. Although you could generate the SELECT with all the replace() calls with help of dynamic SQL.

    0 comments No comments

  5. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-12-03T21:25:02.217+00:00

    You can try to load data into stage tables for these files as string type first and merge these tables/data in a combined table with your desire data type.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.