Question

When importing a file.csv to sql server 2008 I am getting a problem.

In my file.csv the decimal is written in this way: (ex. 1234,34112) and It seems that SQL server does not understand the ',' as decimal.

My solution has been to import it using BULK INSERT as VARCHAR and after that convert it to decimal. It works but I guess it may be a better solution which I am not able to get.

Could you help me with that?

Thanks in advance

Was it helpful?

Solution

There are only two ways of doing it one you have already mentioned importing in Sql server and then doing something like this..

CREATE Table ImportTable (Value NVARCHAR(1000))   --<-- Import the data as it is
INSERT INTO @TABLE VALUES
('1234,34112'),('12651635,68466'),('1234574,5874')

  /* Alter table add column of NUMERIC type.
     And update that column something like this... 
   */

UPDATE ImportTable 
SET NewColumn =  CAST(REPLACE(Value , ',', '.') AS NUMERIC(28,8)) 

Or you can change it your excel sheet before you import it.

Unless you are using SSIS to import data it is always best to get your data in Sql Server 1st using lose datatypes and then do any data manipulation if needed.

OTHER TIPS

SQL Server Management Studio 17 provides a new direct option to import flat files that handles import of decimal csv columns for you. Right your database, then click Tasks > Import Flat File...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top