Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12345»»»

T-SQL Expand / Collapse
Author
Message
Posted Tuesday, February 17, 2009 4:01 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Friday, April 11, 2014 6:10 AM
Points: 1,135, Visits: 317
That gets my vote !

I've got two
Post #658344
Posted Tuesday, February 17, 2009 4:11 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, April 14, 2014 3:48 AM
Points: 987, Visits: 858
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


Post #658353
Posted Tuesday, February 17, 2009 5:59 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld 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!;)
Post #658413
Posted Tuesday, February 17, 2009 6:12 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Saturday, March 29, 2014 11:41 AM
Points: 753, Visits: 133
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
Post #658419
Posted Tuesday, February 17, 2009 6:16 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:49 PM
Points: 5,794, Visits: 8,007
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
Post #658422
Posted Tuesday, February 17, 2009 6:16 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall 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




Post #658423
Posted Tuesday, February 17, 2009 6:18 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 3:49 PM
Points: 5,794, Visits: 8,007
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
Post #658424
Posted Tuesday, February 17, 2009 6:22 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 9:12 AM
Points: 1,887, Visits: 1,179
Do the QotDs get moderated/checked at all? This would have prevented much wailing and gnashing of teeth.
Post #658427
Posted Tuesday, February 17, 2009 6:27 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 9:12 AM
Points: 1,887, Visits: 1,179
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?
Post #658433
Posted Tuesday, February 17, 2009 6:32 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, April 03, 2014 3:33 PM
Points: 2,526, Visits: 530
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
MCTS: SQL Server 2008, Implementation and Maintenance
Post #658436
« Prev Topic | Next Topic »

Add to briefcase ««12345»»»

Permissions Expand / Collapse