Sunday, February 19, 2012

DateDiff Question

When I run the following query, I get an unexpected result. Does anybody
know why?
select
datediff ( ms, '8/23/2005 13:01:08.494', '8/23/2005 13:01:08.510' ),
datediff(ms, '8/23/2005 13:01:08.510', '8/23/2005 13:01:08.791' )
The first datediff gives me 16, which I do expect. The second datediff
gives me 280, I would have thought that it would give me 281.
Is there some kind of rounding going on here?Know your datatypes. Datetime is not as accurate as you assume.
SELECT CAST ('8/23/2005 13:01:08.510' AS DATETIME),
CAST ('8/23/2005 13:01:08.791' AS DATETIME)|||Because sql server represents or stores datetime to an accuracy of one
three-hundredth of a second (3.33 milliseconds).
-- this will be considered as 2005-08-23 13:01:08.790
select cast('8/23/2005 13:01:08.791' as datetime)
AMB
"Scott Evans" wrote:

> When I run the following query, I get an unexpected result. Does anybody
> know why?
> select
> datediff ( ms, '8/23/2005 13:01:08.494', '8/23/2005 13:01:08.510' ),
> datediff(ms, '8/23/2005 13:01:08.510', '8/23/2005 13:01:08.791' )
> The first datediff gives me 16, which I do expect. The second datediff
> gives me 280, I would have thought that it would give me 281.
> Is there some kind of rounding going on here?
>
>

No comments:

Post a Comment