|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 6:37 AM
Points: 990,
Visits: 281
|
|
That gets my vote !
I've got two
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 5:34 AM
Points: 981,
Visits: 821
|
|
As one of the ones who got it "wrong" with the right answer, then I'm posting this just to claim my point too :P
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: Wednesday, August 10, 2011 12:13 PM
Points: 307,
Visits: 565
|
|
I didn't know that SQL Server rounded datetime...this is confusing behavior because I wouldn't have expected to see the '20090201' date in the result set. Try explaining that one to your business users.
If it was easy, everybody would be doing it!;)
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Monday, April 29, 2013 10:56 AM
Points: 751,
Visits: 129
|
|
Given that SQL is logic based, its a shame that the result was so ambiguous...
Like most, I got it wrong too however given the explanation, surely the data capture is between:
2009-01-30 23:59:59.007 AND 2009-01-31 23:59:59.007
and therefore option 'C' is the only correct selection.
Glen Parker
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:31 AM
Points: 5,233,
Visits: 7,033
|
|
Glen Parker (2/17/2009) Given that SQL is logic based, its a shame that the result was so ambiguous...
Like most, I got it wrong too however given the explanation, surely the data capture is between:
2009-01-30 23:59:59.007 AND 2009-01-31 23:59:59.007
and therefore option 'C' is the only correct selection.
Hi Glen,
No, that's not correct. The upper limit in the BETWEEN proposition (2009-01-31 23:59:59.999) has to be converted to a proper datetime value. It can either be rounded down (to 2009-01-31 23:59:99.997), or up (to 2009-02-01 0:00:00.000). The rounding will go to the nearest value that can be represented, so in this case the rounding will be up. After the conversion, the query will work as if it was stated as "... BETWEEN ... AND 2009-02-01 0:00:00.000, and hence the latter datetime value will be returned as well.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 1:42 PM
Points: 3,487,
Visits: 1,581
|
|
Wow! As of 8AM EST, only one person got this question right. As far as I can tell, out of all the answers only two dates are excluded, 2009-01-30 00:00:00.000 and 2009-02-01 00:00:00.003. Which mean multiple answers apply. Did anyone else get the same result?
Greg
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 2:31 AM
Points: 5,233,
Visits: 7,033
|
|
Sam Iam (2/17/2009) I didn't know that SQL Server rounded datetime...this is confusing behavior because I wouldn't have expected to see the '20090201' date in the result set. Try explaining that one to your business users.
Hi Sam,
You don't have to explain this if you always remember the best practice: when filtering on time intervals, avoid BETWEEN. Use >= for the start of the interval, and < (not <= !!) for the end.
That doesn't only prevent this problem, it also saves a lot of headaches and changes if you ever switch to a different datatype (e.g. smalldatetime, or datetime2(7) if you're on SQL Server 2008, etc.)
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Yesterday @ 1:50 AM
Points: 1,449,
Visits: 756
|
|
| Do the QotDs get moderated/checked at all? This would have prevented much wailing and gnashing of teeth.
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Yesterday @ 1:50 AM
Points: 1,449,
Visits: 756
|
|
Hugo Kornelis (2/17/2009)
Hi Glen,
No, that's not correct. The upper limit in the BETWEEN proposition (2009-01-31 23:59:59.999) has to be converted to a proper datetime value. It can either be rounded down (to 2009-01-31 23:59:99.997), or up (to 2009-02-01 0:00:00.000). The rounding will go to the nearest value that can be represented, so in this case the rounding will be up. After the conversion, the query will work as if it was stated as "... BETWEEN ... AND 2009-02-01 0:00:00.000, and hence the latter datetime value will be returned as well.
Does this depend on the Collation Set being used and/or the regional settings on the server/PC because I've just run the OPs code and I don't get 2009-02-01 returned at all?
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 5:31 AM
Points: 2,226,
Visits: 438
|
|
But you still have to remember that inserted/updated values will also be rounded. So if you try to insert '2009-01-31 23:59:59.999' it will be rounded to the next day. If you expect the data to end upp in '2009-01-31' you will get the wrong result. But probably it is no big issue if the date is rounded.
/Håkan
/Håkan Winther MCITP:Database Developer 2008
|
|
|
|