Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Converting Long to readable datetime problem

 
   Database Help (Home) -> MSEQ RSS
Next:  help with licence  
Author Message
DonaldFung

External


Since: Nov 20, 2008
Posts: 1



(Msg. 1) Posted: Thu Nov 20, 2008 7:04 pm
Post subject: Converting Long to readable datetime problem
Archived from groups: microsoft>public>sqlserver>mseq (more info?)

There is a MS-SQL 2005 server and contains a field called modtime that is
numeric(19)

select modtime from [usera].[dbo].[xxx]

and get the following output:

1225234572608

In JAVA, I put it into timestamp and it returns:

2008-10-29 06:56:12.608

However, when I run the following SQL, it returns different value.

select convert(datetime,(((1225234572608) / (1000.0 * 60.0 * 60.0 *
24.0)) ), 121)

1938-10-29 22:56:12.607

Both the date and tiume is different.

What is the proper way to convert the Long to Datetime in SQL statement?
Thanks.

 >> Stay informed about: Converting Long to readable datetime problem 
Back to top
Login to vote
Russell Fields

External


Since: Jan 10, 2008
Posts: 608



(Msg. 2) Posted: Fri Nov 21, 2008 11:34 am
Post subject: Re: Converting Long to readable datetime problem [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Donald,

I believe that this is a matter of the 0 time. As you can see, your
minutes, second, milliseconds match up (with a 1 millisecond rounding
difference). So, the question is: What is the origin time that causes the
days and hours to be wrong? By getting the difference between your answers,
we find the origin date for your Java code is:

SELECT DATEADD(hour,datediff(hour,'1938-10-29 22:56:12.607','2008-10-29
06:56:12.608'),0)
-------------
1970-01-01 08:00:00.000

http://archives.postgresql.org/pgsql-bugs/2002-07/msg00117.php
This seems to show that one timezone dependent UNIX zero date (for US
Pacific time) is 1970-01-01 08:00:00.

However, SQL Server's zero date is 1900-01-01 00:00:00.000.

So, you are adding numbers to a different start point. For your case, after
casting to date time, you would need to add the hours from 1900-01-01
00:00:00.000 to 1970-01-01 08:00:00 (613616 hours) to your date. (You also
will need to decide how you will handle time zones, if that is an issue for
you.) This would look like this:

select dateadd(HOUR,613616, CAST(1225234572608.0 / (1000.0*60.0*60.0*24.0)
AS DATETIME))
-----------------------
2008-10-29 06:56:12.607

Time is not the simplest thing.

RLF

"DonaldFung" wrote in message

> There is a MS-SQL 2005 server and contains a field called modtime that is
> numeric(19)
>
> select modtime from [usera].[dbo].[xxx]
>
> and get the following output:
>
> 1225234572608
>
> In JAVA, I put it into timestamp and it returns:
>
> 2008-10-29 06:56:12.608
>
> However, when I run the following SQL, it returns different value.
>
> select convert(datetime,(((1225234572608) / (1000.0 * 60.0 * 60.0 *
> 24.0)) ), 121)
>
> 1938-10-29 22:56:12.607
>
> Both the date and tiume is different.
>
> What is the proper way to convert the Long to Datetime in SQL statement?
> Thanks.
>

 >> Stay informed about: Converting Long to readable datetime problem 
Back to top
Login to vote
Russell Fields

External


Since: Jan 10, 2008
Posts: 608



(Msg. 3) Posted: Fri Nov 21, 2008 11:44 am
Post subject: Re: Converting Long to readable datetime problem [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Of course, for clarity don't use the 613616 magic number in your code. Do
the following instead:

SELECT DATEADD(HOUR,
DATEDIFF(HOUR,'19000101 00:00',
'19700101 08:00'),
CAST(1225234572608.0 / (1000.0*60.0*60.0*24.0)
AS DATETIME))

RLF

"Russell Fields" wrote in message

> Donald,
>
> I believe that this is a matter of the 0 time. As you can see, your
> minutes, second, milliseconds match up (with a 1 millisecond rounding
> difference). So, the question is: What is the origin time that causes
> the days and hours to be wrong? By getting the difference between your
> answers, we find the origin date for your Java code is:
>
> SELECT DATEADD(hour,datediff(hour,'1938-10-29 22:56:12.607','2008-10-29
> 06:56:12.608'),0)
> -------------
> 1970-01-01 08:00:00.000
>
> http://archives.postgresql.org/pgsql-bugs/2002-07/msg00117.php
> This seems to show that one timezone dependent UNIX zero date (for US
> Pacific time) is 1970-01-01 08:00:00.
>
> However, SQL Server's zero date is 1900-01-01 00:00:00.000.
>
> So, you are adding numbers to a different start point. For your case,
> after casting to date time, you would need to add the hours from
> 1900-01-01 00:00:00.000 to 1970-01-01 08:00:00 (613616 hours) to your
> date. (You also will need to decide how you will handle time zones, if
> that is an issue for you.) This would look like this:
>
> select dateadd(HOUR,613616, CAST(1225234572608.0 / (1000.0*60.0*60.0*24.0)
> AS DATETIME))
> -----------------------
> 2008-10-29 06:56:12.607
>
> Time is not the simplest thing.
>
> RLF
>
> "DonaldFung" wrote in message
>
>> There is a MS-SQL 2005 server and contains a field called modtime that is
>> numeric(19)
>>
>> select modtime from [usera].[dbo].[xxx]
>>
>> and get the following output:
>>
>> 1225234572608
>>
>> In JAVA, I put it into timestamp and it returns:
>>
>> 2008-10-29 06:56:12.608
>>
>> However, when I run the following SQL, it returns different value.
>>
>> select convert(datetime,(((1225234572608) / (1000.0 * 60.0 * 60.0 *
>> 24.0)) ), 121)
>>
>> 1938-10-29 22:56:12.607
>>
>> Both the date and tiume is different.
>>
>> What is the proper way to convert the Long to Datetime in SQL statement?
>> Thanks.
>>
>
 >> Stay informed about: Converting Long to readable datetime problem 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Error converting data type nvarchar to datetime - Good morning :) * Using QL Server 2005 I have created a stored procedure (first time I have done this). It worked fine when I only had 1 variable (the ProjectCode variable). However when I add in the PostedDate variable and try and use it I get the ...

datetime comparison broken??? - I've spent a few hours researching the datetime comparison, and from what I understand, what I'm doing *should* be working... any help is GREATLY appreciated. I have a table that stores a column (EventDateTime) in the datetime format. If I run a..

Converting integer time - We have a SQL 2005 database that has the time stored as an integer. I'm assuming it is the seconds after midnight. Is there a function to convert this to a readable time? Thanks -- Stan Gosselin

Simple Query problem - Sample table as follows Order ID Stock Code Status --------- ------- ------- 203 STK1 3 203 STK2 2 203 STK4 3 204 STK1 3 204 STK5 3 205 ..

index on a view - Hello, I have a table (TAB) and A View with alias (VIEW) Table cod varchar 3 descr carchar 60 my view cod alias COd1 descr alias DES Now i need a index on view with key COD1 I can't create it. Can..
   Database Help (Home) -> MSEQ All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]