Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert scientific notation back to numbers in SQL Server

Tags:

sql

sql-server

I had data in Excel like

7540006
7540447

But when I import the data into SQL Server, it is saved as

7.54001e+006
7.54045e+006

So now when I try to convert it to back to original state, it's not ending up with the correct value.

I have tried following queries for conversion

declare @a varchar(40)
set @a = '7.54001e+006'

declare @b decimal(27, 12)

SELECT @b = CONVERT(REAL, @a, 2)

SELECT LTRIM(RTRIM(str(@a))), LTRIM(STR(@a))

SELECT CAST('7.54001e+006' as REAL)

and the output I am getting is addition of 3 to original value for all methods i.e.

7540010
7540050

How do I convert it back to original state ??

like image 556
Mahajan344 Avatar asked May 27 '16 15:05

Mahajan344


People also ask

How do you undo scientific notation?

To change scientific notation to standard notation, we reverse the process, moving the decimal point to the right. Add zeros to the end of the number being converted, if necessary, to produce a number of the proper magnitude.

How do you change scientific notation to decimal notation?

Convert scientific notation to decimal formDetermine the exponent, n , on the factor 10 . Move the decimal n places, adding zeros if needed. If the exponent is positive, move the decimal point n places to the right. If the exponent is negative, move the decimal point |n| places to the left.

What is To_number in SQL Server?

TO_NUMBER converts a string to a number of data type NUMERIC. TO_CHAR performs the reverse operation; it converts a number to a string. CAST and CONVERT can be used to convert a string to a number of any data type. For example, you can convert a string to a number of data type INTEGER.

How do I convert int to numeric in SQL?

Use the CAST() function to convert an integer to a DECIMAL data type. This function takes an expression or a column name as the argument, followed by the keyword AS and the new data type. In our example, we converted an integer (12) to a decimal value (12.00).


2 Answers

Try the following query which gives the exact value

select CAST(CAST('7.54001e+006'  AS FLOAT) AS bigint)
like image 194
Nithila Avatar answered Oct 17 '22 18:10

Nithila


All data is stored as Unicode string 255 (DT_WSTR) in excel.

Read excel data as Unicode form. then do conversion in ssis or database using.

SELECT CAST('7.54001e+006' as REAL)

In excel data source >> connection manager >>Data access mode == QUERY

     SELECT * FROM [SheetName$]
like image 36
sandeep rawat Avatar answered Oct 17 '22 16:10

sandeep rawat