3

I have a query like the one described below.

select CAST(0x83360B00 AS Date)

When I run this query in SQL server, I am able to get result in date format as:

2012-12-15

All I want to know is how this is being generated. Thanks in advance.

Rizvi
  • 287
  • 3
  • 14
vstandsforvinay
  • 138
  • 1
  • 11

2 Answers2

6

Sql server stores the internal representation of the DATE data type as the big endian count of days from 1 Jan 0001.

So 0x83360B00 is

SELECT 0x83 + 0x36 * 256 + 0x0B * 65536
= 734851 days

Add these back:

DECLARE @Date DATE = '1 Jan 0001'
SELECT DATEADD(dd, 734851, @Date)

Which returns:

2012-12-15
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • Although as a human, I much prefer `2012-12-15` to `0x83360B00` :). I guess the [Gregorian calendar](http://en.wikipedia.org/wiki/Gregorian_calendar) doesn't make sense prior to 1600 or so. – StuartLC May 02 '14 at 07:14
0

Probably with the corresponding cast to varbinary:

SELECT CAST(CAST('2012-12-15' AS DATE) AS VARBINARY)
dean
  • 9,960
  • 2
  • 25
  • 26