[quoted text, click to view] Ian Boyd wrote:
> 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?
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
--