Groups | Blog | Home
all groups > sql server (alternate) > june 2004 >

sql server (alternate) : HELP with TIME Calculations


Joe User
6/30/2004 11:45:17 AM
I am looking to calculate the difference between and event time and a sample
time of Now. This is the query that I thought would do it, however I'm
returning DIFFERENCE values that look the same when the calcuation is being
made on different EVENT_TIME values....

I thought I knew how DateDiff worked, but apparently not.

select GetDate()as NOW,event_time,Datediff(s,(Cast(event_time as
Numeric)),(Cast(GetDate() as Numeric))) as DIFFERENCE
FROM events


-----------NOW----------|-----EVENT_TIME------|-DIFFERENCE
2004-06-30 11:22:38.560 1999-10-30 23:51:37.000 147225600
2004-06-30 11:22:38.560 1999-10-30 23:23:47.000 147225600
2004-06-30 11:22:38.560 1999-10-30 06:49:38.000 147312000
2004-06-30 11:22:38.560 1999-10-30 06:50:00.000 147312000
2004-06-30 11:22:38.560 1999-10-30 06:50:41.000 147312000
2004-06-30 11:22:38.560 1999-10-30 06:49:59.000 147312000
2004-06-30 11:22:38.560 1999-10-30 06:49:58.000 147312000
2004-06-30 11:22:38.560 1999-10-30 06:50:53.000 147312000
2004-06-30 11:22:38.560 1999-10-30 06:50:46.000 147312000
2004-06-30 11:22:38.560 1999-10-30 06:49:42.000 147312000
2004-06-30 11:22:38.560 1999-10-30 06:50:36.000 147312000
2004-06-30 11:22:38.560 1999-10-30 06:50:07.000 147312000
2004-06-30 11:22:38.560 1999-10-30 10:54:37.000 147312000
2004-06-30 11:22:38.560 1999-10-30 11:40:15.000 147312000
2004-06-30 11:22:38.560 1999-10-30 09:52:51.000 147312000
2004-06-30 11:22:38.560 1999-10-30 12:12:46.000 147225600
2004-06-30 11:22:38.560 1999-10-30 12:32:45.000 147225600
2004-06-30 11:22:38.560 1999-10-30 12:32:45.000 147225600
2004-06-30 11:22:38.560 1999-10-30 12:32:46.000 147225600
2004-06-30 11:22:38.560 1999-10-30 12:46:30.000 147225600
2004-06-30 11:22:38.560 1999-10-30 15:31:25.000 147225600
2004-06-30 11:22:38.560 1999-10-30 23:08:25.000 147225600
2004-06-30 11:22:38.560 1999-10-30 16:35:51.000 147225600


Can someone help?
TIA!

Joe..

John Gilson
6/30/2004 9:33:17 PM
[quoted text, click to view]

DATEDIFF returns the difference between 2 datetime values with respect
to a specified unit of time, e.g., difference between 2 datetime values in
seconds. See Books Online (BOL), the SQL Server online documentation,
for more information. To get the number of seconds between 2 datetime values,
try

SELECT CURRENT_TIMESTAMP AS now,
event_time,
DATEDIFF(SECOND, event_time, CURRENT_TIMESTAMP) AS
difference
FROM Events

CURRENT_TIMESTAMP is the Standard SQL way of getting the current
date and time, functionally equivalent to GetDate which is specific to T-SQL.

--
JAG

Erland Sommarskog
6/30/2004 9:57:34 PM
Joe User (joe@user.com) writes:
[quoted text, click to view]

Now, pray tell, why did you throw in Numeric into the lot? This is the
source of your problem. If you say Numeric without specifying precision
and scale, you get a default precision which is 18 and a default scale
of 0. This means that you are throwing the time portions out the window.

I don't really which scale you need to use to not lose precision, but
then again, if you just say

select GetDate()as NOW, event_time,
Datediff(s, event_time, getdate()) as DIFFERENCE
FROM events

it's even simpler.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
AddThis Social Bookmark Button