Solved

Convert BIGINT to DATETIME, but losing precision

Posted on 2013-05-21
11
336 Views
Last Modified: 2013-05-21
select DATEADD(s,(1369087430101949/1000000),'1/1/1970')

this returns:  2013-05-20 22:03:50.000

I need the precision beyond the second.  

Any tips?
0
Comment
Question by:dbaSQL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 17

Author Comment

by:dbaSQL
ID: 39184619
To clarify, I am converting the BIGINT to DATETIME, but I am losing the precision in my conversion.  I need to prevent that loss of precision.
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39184632
millisecond?

select DATEADD(millisecond,(1369087430101949/1000000000),'1970-01-01')
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39184637
>> I need the precision beyond the second.

Then why are you using second as the interval?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39184639
Also, you are doing integer division.  Try:

select DATEADD(millisecond,(1.0*1369087430101949/1000000000),'1970-01-01')
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 39184670
i tried ms as my interval, but my output turns into this:    1970-01-16 20:18:07.430
select DATEADD(ms,(1369087430101949/1000000),'1/1/1970')

I tried your suggestion, matthews, but it returns this:
1970-01-01 00:22:49.087

Paul, yours does the same -- 1970-01-01 00:22:49.087
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39184686
dateadd supports these:
millisecond  ms
microsecond mcs
nanosecond ns
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 39184692
yes.  but the output with ms is 1970, rather than 5/20.

select DATEADD(s,(1369087430101949/1000000),'1/1/1970')
returns:  2013-05-20 22:03:50.000

select DATEADD(ms,(1369087430101949/1000000),'1/1/1970')
returns: 1970-01-16 20:18:07.430
0
 
LVL 17

Author Comment

by:dbaSQL
ID: 39184714
do you have a suggestion?
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 250 total points
ID: 39184726
I'm not able to convert the value directly into a datetime at the millisecond level as I keep getting an overflow condition.  But I can convert the value to the datetime that you're getting, and add the milliseconds in as a separate call.

select
  DATEADD (millisecond, 1369087430101949 % 1000000,
  DATEADD(s,1369087430101949/1000000, '1/1/1970'))



Kent
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 39184733
select
dateadd(ms,(1369087430101949 % 1000000),
dateadd(s,round(1369087430101949 / 1000000,0),'1970-01-01')
        )
, convert(varchar,
          dateadd(ms,(1369087430101949 % 1000000),
dateadd(s,round(1369087430101949 / 1000000,0),'1970-01-01')
        )
, 14)

beat me to it :(
0
 
LVL 17

Author Closing Comment

by:dbaSQL
ID: 39184886
A little closer, but both of yours were only giving me millis, not micros.  

I used this:
SELECT DATEADD (mcs,(1369087430101949 % 1000000),CAST(DATEADD(s,(1369087430101949/1000000), '1/1/1970') AS DATETIME2))

Thank you both for your time.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question