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 «««123

DateAdd Function Expand / Collapse
Author
Message
Posted Wednesday, March 4, 2009 11:43 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 9:23 AM
Points: 2,335, Visits: 2,670
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


-------------------
"Operator! Give me the number for 911!" - Homer Simpson

"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
Post #668548
Posted Wednesday, March 4, 2009 11:50 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Sunday, April 27, 2014 6:26 AM
Points: 1,521, Visits: 3,036
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.
Post #668555
Posted Thursday, March 5, 2009 5:52 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 8, 2013 4:59 AM
Points: 196, Visits: 79
That was too easy question.
Post #669121
Posted Tuesday, March 10, 2009 1:31 PM


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: Thursday, July 24, 2014 8:36 AM
Points: 772, Visits: 1,183
I thought this was a trick question, but got it right anyway, whew~

SQLServerNewbie

MCITP: Database Administrator SQL Server 2005
Post #672737
Posted Monday, November 30, 2009 9:33 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 19, 2012 10:59 PM
Points: 1, Visits: 4
answer c
Post #826474
Posted Tuesday, February 12, 2013 10:42 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, July 25, 2014 12:34 AM
Points: 1,888, Visits: 2,326
easy one +1


_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1419296
« Prev Topic | Next Topic »

Add to briefcase «««123

Permissions Expand / Collapse