Peter Larsson Blog

Patron Saint of Lost Yaks

The internal storage of a SMALLDATETIME value

SELECT  [Now],
        BinaryFormat,
        SUBSTRING(BinaryFormat, 1, 2) AS DayPart,
        SUBSTRING(BinaryFormat, 3, 2) AS TimePart,
        CAST(SUBSTRING(BinaryFormat, 1, 2) AS INT) AS [Days],
        DATEADD(DAY, CAST(SUBSTRING(BinaryFormat, 1, 2) AS INT), 0) AS [Today],
        SUBSTRING(BinaryFormat, 3, 2) AS [Ticks],
        DATEADD(MINUTE, CAST(SUBSTRING(BinaryFormat, 3, 2) AS SMALLINT), 0) AS Peso
FROM    (
            SELECT  CAST(GETDATE() AS SMALLDATETIME) AS [Now],
                    CAST(CAST(GETDATE() AS SMALLDATETIME) AS BINARY(4)) AS BinaryFormat
        ) AS d
 

 

Legacy Comments


Rob Volk
2010-12-15
re: The internal storage of a SMALLDATETIME value
Have you done this for the datetime2 types? It's pretty interesting. :)

Peso
2010-12-15
re: The internal storage of a SMALLDATETIME value
Done. Have a look at it here
weblogs.sqlteam.com/...