Groups | Blog | Home
all groups > sql server programming > july 2006 >

sql server programming : Datetime math gives strange results


David Portas
7/9/2006 8:08:45 AM
[quoted text, click to view]

What is "double precision"? Not a SQL Server datatype. So I'm assuming
it is FLOAT / REAL, which are inexact types. If you want a rounded
result then use ROUND and case to an exact type like NUMERIC. Also, use
DATEDIFF/DATEADD rather than add and subtract dates with the arithmetic
operators.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--
Ian Boyd
7/9/2006 10:56:42 AM
Consider:

CREATE TABLE #foo (StartDateTime datetime, EndDateTime datetime)
INSERT INTO #foo (StartDateTime, EndDateTime) VALUES ('2006-06-26
15:54:03.000', '2006-06-26 15:58:33.000')

SELECT
StartDateTime,
EndDateTime,
DATEDIFF(minute, StartDateTime, EndDateTime) AS MinutesDiff,
CAST((EndDateTime - StartDateTime) AS double precision)*24.0*60.0 AS
DaysToMinutes,

DATEDIFF(second, StartDateTime, EndDateTime) AS SecondsDiff,
CAST((EndDateTime - StartDateTime) AS double precision)*24.0*60.0*60.0 AS
DaysToSeconds
FROM #foo

DROP TABLE #foo


The results of the select are:

StartDateTime: 2006-06-26 15:54:03.000
EndDateTime: 2006-06-26 15:58:33.000

Minutes Diff: 4
DaysToMinutes: 4.5000000000000009 <===0.5 too high

SecondsDiff: 270
DaysToSeconds: 270.00000000000006 <=== correct to 1E-13


How is it that the same math that results in seconds being correct to the
13th decimal place makes the minutes off in the first decimal place? It
can't be the math engine and rounding errors. It must be something like leap
minutes now in June or something. But even so, the math is unaffected by
that.

So what's going on?

Kalen Delaney
7/9/2006 11:27:05 AM
Hi Ian

Yes, if you look at the BOL description for Datediff, it reports that the
return value is INTEGER.

This is true for any date part, and can be very confusing until you figure
it out.
For example, the difference in years between Jan 1, 2006 and Dec 31, 2006 is
0 (no year boundary crossed)
but the difference in years between Dec 31, 2006 and Jan 1, 2007 is 1.

--
HTH
Kalen Delaney, SQL Server MVP


[quoted text, click to view]

Ian Boyd
7/9/2006 12:10:31 PM
i figured it out.

DATEDIFF(minute) doesn't return the number of minutes between two dates, it
returns "the number of date and time boundaries crossed between two
specified dates"

It returns whole numbers, when i assumed a fractional result.

e.g.

DECLARE @d1 datetime
DECLARE @d2 datetime
SET @d1 = '2006-07-04'
SET @d2 = '2006-07-04 6:30:00.000'
SELECT @d1, @d2

--Number of hours between midnight and 6:30am : 6.5
--Difference between midnight and 6:30am in hours: 6

--Number of hour units crossed by midnight and 6:30AM
SELECT DATEDIFF(hour, @d1, @d2) --6

--Number of hours between midnight and 6:30AM
SELECT CAST(@d2-@d1 AS double precision)*24 --6.5

Ian Boyd
7/9/2006 12:10:34 PM
[quoted text, click to view]

From BOL:
The Microsoft® SQL ServerT float[(n)] data type conforms to the SQL-92
standard for all values of n from 1 to 53.
The synonym for double precision is float(53).

Tracy McKibben
7/9/2006 1:48:55 PM
[quoted text, click to view]

From the same BOL entry that you quoted:
float and real
Approximate number data types for use with floating point numeric data.
Floating point data is approximate; not all values in the data type
range can be precisely represented.



--
Tracy McKibben
MCDBA
Helmut Woess
7/9/2006 5:20:30 PM
Am Sun, 9 Jul 2006 10:56:42 -0400 schrieb Ian Boyd:

....
[quoted text, click to view]
Why too high? Your dates have 270 seconds = 4.5 minutes between.
Change time of EndDateTime from 15:58:33.000 to 15:58:03.000 and you will
get 4.0
If you use float for your calculation you cannot await a rounded integer as
result.

Ian Boyd
7/9/2006 10:20:52 PM
[quoted text, click to view]

i actually knew that about DateDiff, but it's just been a while since i used
it i forgot.

And the name "Date Diff" leads you to believe something else :)

melissamarasigan NO[at]SPAM gmail.com
7/10/2006 12:13:56 AM

Datetime math gives the results using the SQL Statements. First you
should know the different SQL Satetements and functions used in MS SQL
Server and Oracle SQL
AddThis Social Bookmark Button