title: How does SQL Server store decimal type values internally?
body: <p>In SQL Server you can use <code>FLOAT</code> or <code>REAL</code> to store floating point values the storage format of which is cleared defined by the IEEE 754 standard. For fixed point values we can use <code>DECIMAL</code> type (which has a synonym <code>NUMERIC</code>). However I'm not pretty sure how SQL Server store <code>DECIMAL</code> values internally. For example, if I define a table and insert a row like this:</p>
  
  <pre><code>IF OBJECT_ID('dbo.test_number_types') IS NOT NULL DROP TABLE dbo.test_number_types;
  CREATE TABLE dbo.test_number_types
  (
      id INT IDENTITY(1, 1),
      c1 NUMERIC(5, 4)
  )
  GO
  
  INSERT INTO dbo.test_number_types(c1)VALUES(5.7456);
  </code></pre>
  
  <p>When I use <code>DBCC PAGE</code> command to check how SQL Server stores the number 5.7456, I got this:</p>
  
  <pre><code>01 70 E0 00 00
  </code></pre>
  
  <p>This hex string should use little endian. I can't figure out how SQL Server turns 5.7456 into <code>01 70 E0 00 00</code> and how it decides how many bytes are for the integral part and how many bytes are for the decimal parts. Can anyone help?</p>
  
  <hr>
  
  <p>BTW, I've checked the book "SQL Server 2012 Internals". There is a chapter dedicated to data type storage. But it seems <code>DECIMAL</code> type storage is not mentioned in the book.</p>