Sql-server – Why is datalength of decimal 5 regardless of precision

datatypesdecimalsql serversql-server-2005

I'm trying to better understand numeric types in SQL and have read that the decimal type will always require 17 bytes. However, the MS Docs list a table indicating the amount of space used depends on the decimal's precision. So I tried to test it using the datalength function.

create table tbl_TestDec(dec1 decimal(19,4), dec2 decimal(20,4), dec3 decimal(9,4))

insert into tbl_TestDec
select 1, 1, 1

select datalength(dec1), datalength(dec2), datalength(dec3) from tbl_TestDec

This outputs:

5   5   5

I was expecting either 9 13 5 or 17 17 17. I'm using SQL Server 2005. Are all decimals vardecimal or am I misunderstanding the datalength function?

Best Answer

I think you will find quite a bit of interesting information about this topic in Reducing Database Size by Using Vardecimal Storage Format, but to answer your question (quoting from the post, highlighting mine):

The storage size depends on both the declared precision of the column AND the value. For example, if you have a decimal column declared with precision 18 and the common value for this column has only 3 digits, SQL Server uses only 5 bytes, which is almost 50% less than the 9 bytes used by the fixed format. But, if the common value for the column has 18 digits, SQL Server uses 11 bytes, which is 20% more than the corresponding size in the fixed-length format.

Try inserting values with larger precision and your results will change.

On another note:

You can enable Vardecimal storage at the database and table level. Using SSMS Object Explorer, you can look at your database options and table storage properties to verify whether you have Vardecimal storage enabled. You can also use sp_db_vardecimal_storage_format, which notes:

Returns the current vardecimal storage format state of a database or enables a database for vardecimal storage format. Starting with SQL Server 2008, user databases are always enabled. Enabling databases for the vardecimal storage format is only necessary in SQL Server 2005.

That said, Enterprise-only vardecimal storage was deprecated in SQL Server 2008 in favour of row compression, which is a superset of vardecimal storage capabilities. Starting with SQL Server 2016 SP1, row and page data compression are available in all editions, and do not require a database configuration option to use.