Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are the "money" and "decimal" data types in SQL Server stored in memory?

I want to know about the storage SQL Server data types in memory.

How is the money data type in SQL Server stored in memory? I know that money is stored in 8 bytes and smallmoney is stored in 4 bytes. But I don't know how?

For example when you have 123400.93 for the money, how is it stored in 8 bytes?

I have the same question about the decimal and DATE data types.

Especially for DATE, the format is YYYY-MM-DD, but how is it stored in 3 bytes? Is it stored as described here: http://dev.mysql.com/doc/internals/en/date-and-time-data-type-representation.html or the number of days from a specific day is stored?

like image 519
Nooshin Avatar asked Oct 22 '13 18:10

Nooshin


People also ask

How are decimals stored in SQL?

Standard SQL requires that DECIMAL(5,2) be able to store any value with five digits and two decimals, so values that can be stored in the salary column range from -999.99 to 999.99 . In standard SQL, the syntax DECIMAL( M ) is equivalent to DECIMAL( M ,0) .

How is SQL data stored in memory?

SQL Server stores its data in 8KB data pages. As these pages are read off disk they are stored in memory. This is referred to as buffer memory. A list of all the data pages in memory is stored in the dynamic management view sys.

What is the data type for money in SQL Server?

The MONEY data type stores currency amounts. TLike the DECIMAL(p,s) data type, MONEY can store fixed-point numbers up to a maximum of 32 significant digits, where p is the total number of significant digits (the precision) and s is the number of digits to the right of the decimal point (the scale).

What is the difference between money and decimal data types in SQL Server?

Money is stored in the same way an integer is stored, whereas decimal is stored as a decimal point and decimal digits. This means that money will drop accuracy in most cases, while decimal will only do so when converted back to its original scale. Money is fixed point, so its scale doesn't change during calculations.


1 Answers

Just adding a bit here...

A single byte is made up of 8 bits. A bit can hold 2 values (0 or 1).

So 4 bytes is 32 bits (4 x 8). Which means the numeric range of what can be stored is from 0 to 2^32 which gives a total range of 4,294,967,296 values.

smallmoney is signed so we drop one of the bits to be used for the sign, which leaves 2^31, or 2,147,483,648 possible values and the +/- sign.

Now we take into account that the last 4 digits of a money type are always after the decimal point and we end up with a range of -214,748.3648 to 214,748.3647

Technically, money and smallmoney values are stored by flipping bits in a word or byte just like everything else. If you need more info, read http://computer.howstuffworks.com/bytes.htm

Or you might see this for the possible value range of money and smallmoney: http://technet.microsoft.com/en-us/library/ms179882.aspx

update
For the DATETIME data type, it's the same concept with a little twist. In MS SQL a DATETIME is stored using 2 numbers. The first is number of days since 1/1/1900 and the second is number of ticks since midnight: See http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/how-are-dates-stored-in-sql-server and When storing a datetime in sql server (datetime type), what format does it store it in?

like image 81
NotMe Avatar answered Oct 19 '22 17:10

NotMe