I am using openrowset to import a csv file into SQL Server. One of the columns in the csv file contains numbers in scientific notation (1.08E+05) and the column in the table it is being inserted
By default it is importing the value as 1 and ignoring the .08E+05.
I have tried using cast() and convert() to convert the value directly when the query is executed as well as setting up the datatype in the table as a character string and importing it as such. All of these methods have the same behavior where the .08E+05 is ignored.
Is there a way to have the value imported as 108000 instead of 1 without the .08E+05 without having to change the csv file itself?
Setting up the datatype as a varchar and reading in the csv file appears to have the same effect with the following code:
CREATE TABLE #dataTemp (StartDate datetime, Value varchar(12))
SET @insertDataQuery = 'SELECT Date, CSVValue from OpenRowset(''MSDASQL'', ''Driver={Microsoft Text Driver (*.txt; *.csv)}; DefaultDir='
SET @insertDataQuery = @insertDataQuery + 'C:\Data\;'',''SELECT * FROM '+ '11091800.csv' + ''')'
INSERT INTO #dataTemp EXEC(@insertDataQuery)
SELECT * FROM #dataTemp
Not all of the values in the CSV file have the scientific notation and the value without it, e.g. 81000 come across without issue.
Will casting it as a real work?
select cast('1.08E+05' as real)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With