|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 3:05 PM
Points: 2,117,
Visits: 2,209
|
|
jpowers (3/4/2009) I thought it was a dead giveaway that only one answer had the correct date prefix (2009-02-09). The wrong answers had 2009-02-10 and 2009-02-08. Except for clicking on the wrong button on mistake, I can't understand why anyone would miss this question.
I got the original question correct, not because of the date but simply by doing the calculations in my head (in this case that was relatively easy for me). I almost second-guessed myself because it seemed uncharacteristic that the obvious QOTD answer was the correct one. Usually there is some trick involved.
But I got the second question (the one posted in the comments) wrong, because of the way SQL Server rounds milliseconds. Go figure, as they say....
webrunner
------------------- "The chemistry must be respected." - Walter White
"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'" Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
Webrunner (and others following the subthread on my supplemental quiz question),
The rounding of milliseconds in DATETIME data is to .000, .003, or .007. Keep this in mind when designing queries that test for a time being within a range. An easy error to make would be to try to define a day with the BETWEEN operator. If we want to see if a time value is on the same day as some other time, you may be tempted to strip that target datetime down to just the date and then append a "highest possible" time value.
With this code, a Time_1 value of '2009-03-05 00:00:00.001' would return true, probably not what you'd want.
declare @time_1 datetime declare @startTime datetime declare @endTime datetime set @time_1 = '2009-03-05 00:00:00.001' set @startTime = '2009-03-04 00:00:00.00' set @endTime = '2009-03-04 23:59:59.999' Print '@startTime is ' + convert(char(23),@startTime,121) Print '@time_1 is ' + convert(char(23),@time_1,121) Print '@endTime is ' + convert(char(23),@endTime,121) If @time_1 between @startTime and @endTime print 'True'
Play with the values of @time_1 to understand the effect of rounding.
A better date-range test would be to test for greater than or equal to the start of a date and less than the start of the next date.
declare @time_1 datetime set @time_1 = '2009-03-05 00:00:00.001' If @time_1 >= '2009-03-04' and @time_1 < '2009-03-05' -- time portion defaults to zeroes print 'True' else print 'False'
One final note: SQL 2008 introduces some new data types that allow separation of date and time and also greater precision on time.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, April 08, 2013 4:59 AM
Points: 196,
Visits: 79
|
|
| That was too easy question.
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Wednesday, May 08, 2013 3:02 PM
Points: 768,
Visits: 1,159
|
|
I thought this was a trick question, but got it right anyway, whew~
SQLServerNewbie
MCITP: Database Administrator SQL Server 2005
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, July 19, 2012 10:59 PM
Points: 1,
Visits: 4
|
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 11:16 PM
Points: 1,061,
Visits: 1,151
|
|
easy one +1
|
|
|
|