Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is date conversion being done in sql server

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.

like image 797
vstandsforvinay Avatar asked Jul 17 '26 14:07

vstandsforvinay


1 Answers

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
like image 189
StuartLC Avatar answered Jul 20 '26 02:07

StuartLC