Question

What is the best data type in SQL Server 2008 R2 to store a value like 1.74269E-06?

I am trying to put as float, but the value always zero. Advice please.

TIA.

Regards,

Was it helpful?

Solution

When I insert that value into a datatype of float, it isn't zero.

You might be accidentally casting it to INT or displaying it incorrectly.

If your values are going to be very large or very small (and can't be held in DECIMAL) then you'll need to use FLOAT

DECLARE @Table TABLE (F FLOAT, D DECIMAL(19,7))

INSERT INTO @Table (F,D)
VALUES(1,1)

INSERT INTO @Table (F,D)
VALUES(1.74269E-06,1.74269E-06)

-- I can see the float here in exponential format
SELECT F,D FROM @Table

-- I can see it here in decimal format
SELECT CONVERT(DECIMAL(19,7),F),D FROM @Table

-- But if I cast to INT it disappears.
SELECT CONVERT(INT,F),D FROM @Table

OTHER TIPS

Use decimal instead.

decimal [ (p[ ,s] )] and numeric[ (p[ ,s] )] Fixed precision and scale numbers. When maximum precision is used, valid values are from - 10^38 +1 through 10^38 - 1. The ISO synonyms for decimal are dec and dec(p, s). numeric is functionally equivalent to decimal.

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