English 中文(简体)
Most efficient way to store number 11.111 in SQL Server
原标题:

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

最佳回答

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.

问题回答

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.

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.

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.

Use the decimal data type to set exact precision:

decimal(5,3)




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签