[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Karch

3/23/2007 11:43:00 PM

So, what is actually happening here?

DECLARE @datenow as DATETIME;

DECLARE @dateBinary as VARBINARY(MAX);

SELECT @datenow = '3/23/2007 12:00:00AM';

SELECT @dateBinary = CAST( @datenow AS VARBINARY(MAX))

SELECT @dateBinary

@dateBinary = '0x000098FA00000000'

(using a date of '3/22/2007 12:00:00AM' will produce '0x000098F900000000'
as the binary representation)

What is this binary value that SQL Server is producing; how is it produced?
I am trying to produce the same result in code when serializing a C#
datetime datatype.

Thanks!


3 Answers

Hugo Kornelis

3/24/2007 12:18:00 AM

0

On Fri, 23 Mar 2007 18:43:15 -0500, Karch wrote:

>So, what is actually happening here?
>
>DECLARE @datenow as DATETIME;
>
>DECLARE @dateBinary as VARBINARY(MAX);
>
>SELECT @datenow = '3/23/2007 12:00:00AM';
>
>SELECT @dateBinary = CAST( @datenow AS VARBINARY(MAX))
>
>SELECT @dateBinary
>
>@dateBinary = '0x000098FA00000000'
>
>(using a date of '3/22/2007 12:00:00AM' will produce '0x000098F900000000'
>as the binary representation)
>
>What is this binary value that SQL Server is producing; how is it produced?
>I am trying to produce the same result in code when serializing a C#
>datetime datatype.
>
>Thanks!
>

Hi Karch,

As mentioned in Books Online, a datetime is stored as two integer
values. The first is the number of days since Jan 1st, 1900. For March
23, 2007, this is 39162, or in hex: 0x98FA. The second number is used to
store the time portion as the number of 1/300 second intervals since
midnight.

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hug...

Tom Cooper

3/24/2007 12:36:00 AM

0

The first half is an integer specifying the number of days since Jan 1,
1900. That is, on 3/23/2007 there had been 0x000098FA or 39162 days since
1/1/1900.
So
SELECT DATEADD(d, 39162, '19000101')
will return 2007-03-23 00:00:00.000

The last half is the number of clock ticks since midnight where a clock tick
is 3 and 1/3 milliseconds. So, for example,
select cast(0x000098FA00000000 as datetime)
select cast(0x000098FA00000001 as datetime)
select cast(0x000098FA00000002 as datetime)
select cast(0x000098FA00000003 as datetime)

will return
2007-03-23 00:00:00.000
2007-03-23 00:00:00.003
2007-03-23 00:00:00.007
2007-03-23 00:00:00.010

Please note that the internal formats used to store data in SQL Server are
not documented. So, at least in theory, they might change in some future
release. Also, some bit patterns are not valid as datetimes. For example,
0x002D248000000000 (which would imply 0x002D2480 or 2,958,464 days after
1/1/1900 or Jan 1, 10000) is not a valid SQL Server date and unexpected
things can happen if you succeed in storing that value in a datetime column.

Tom

"Karch" <nospam@absotutely.com> wrote in message
news:u2DQBUabHHA.1300@TK2MSFTNGP02.phx.gbl...
> So, what is actually happening here?
>
> DECLARE @datenow as DATETIME;
>
> DECLARE @dateBinary as VARBINARY(MAX);
>
> SELECT @datenow = '3/23/2007 12:00:00AM';
>
> SELECT @dateBinary = CAST( @datenow AS VARBINARY(MAX))
>
> SELECT @dateBinary
>
> @dateBinary = '0x000098FA00000000'
>
> (using a date of '3/22/2007 12:00:00AM' will produce '0x000098F900000000'
> as the binary representation)
>
> What is this binary value that SQL Server is producing; how is it
> produced? I am trying to produce the same result in code when serializing
> a C# datetime datatype.
>
> Thanks!
>
>


Karch

3/24/2007 1:52:00 AM

0

Thanks! I don't know how I missed that in my searches...

"Hugo Kornelis" <hugo@perFact.REMOVETHIS.info.INVALID> wrote in message
news:fdr803dk3f60rrf3ncb76g6qp3i7ulkjge@4ax.com...
> On Fri, 23 Mar 2007 18:43:15 -0500, Karch wrote:
>
>>So, what is actually happening here?
>>
>>DECLARE @datenow as DATETIME;
>>
>>DECLARE @dateBinary as VARBINARY(MAX);
>>
>>SELECT @datenow = '3/23/2007 12:00:00AM';
>>
>>SELECT @dateBinary = CAST( @datenow AS VARBINARY(MAX))
>>
>>SELECT @dateBinary
>>
>>@dateBinary = '0x000098FA00000000'
>>
>>(using a date of '3/22/2007 12:00:00AM' will produce '0x000098F900000000'
>>as the binary representation)
>>
>>What is this binary value that SQL Server is producing; how is it
>>produced?
>>I am trying to produce the same result in code when serializing a C#
>>datetime datatype.
>>
>>Thanks!
>>
>
> Hi Karch,
>
> As mentioned in Books Online, a datetime is stored as two integer
> values. The first is the number of days since Jan 1st, 1900. For March
> 23, 2007, this is 39162, or in hex: 0x98FA. The second number is used to
> store the time portion as the number of 1/300 second intervals since
> midnight.
>
> --
> Hugo Kornelis, SQL Server MVP
> My SQL Server blog: http://sqlblog.com/blogs/hug...