views:

115

answers:

5

What datatype is the most efficient to store a number such as 11.111.

The numbers will have up 2 digits before the point and up to three after.

Currently the column is a 'bigint', I am guessing that this will need to be changed to a decimal or a float.

Any help much appreciated.

thanks

+2  A: 

I would think that you have two options: Multiply by 1000 and store as an int (and divide on the way out), or just store as a double. With memory and drive space being so cheap, it's probably not worth the hassle of optimizing any further.

Darren Oster
When you say a double you mean a float right? Thanks for the help.
Chin
+5  A: 

If you need exact representation, the smallmoney datatype uses 4 bytes.

This is equivalent to using an int (4 bytes), and performing multiplication/division to take care of the fractional digits.

Mitch Wheat
Don't think it gets smaller than this
Am
A: 

Use the decimal data type to set exact precision:

decimal(5,3)
Gordon Bell
uses 5 bytes.......
Mitch Wheat
yes, so smallmoney
Gordon Bell
smallmoney is 4 bytes: http://msdn.microsoft.com/en-us/library/ms179882.aspx
RickNZ
+2  A: 

You should use the DECIMAL data type. It stores exact floating point numbers

DECLARE @d decimal(5, 3)
SET @d=12.671
SELECT @d, DATALENGTH(@d)

As @Mitch pointed out, this would be 5 bytes in size. You pay an extra byte for easier calculations with the value.

Am
uses 5 bytes.......
Mitch Wheat
..and that should be DECIMAL(5,3)
Mitch Wheat
+1  A: 

Bigint is an integer datatype, so yeah, that won't work without some preprocessing footwork. Do you need to store the values precisely, or only approximately? The binary floating point types (float, real) are efficient and compact, but don't represent many decimal values exactly, and so comparisons won't always give you what you expect, and errors accumulate. decimal is a good bet. A decimal of precision 5 and scale 3 (meeting your requirements) consumes five bytes.

See documentation on the decimal datatype here, and datatypes in general here.

Michael Petrotta
Difficult for me to choose a correct answer, but I found this the most helpful. Maybe my question wording wasn't so great. Thanks
Chin