November 17, 2011 at 9:14 am
THis was a really simple one - thanks!
November 17, 2011 at 9:20 am
good question!!!
November 17, 2011 at 9:58 am
nice easy question today - tks
November 17, 2011 at 10:56 am
There are good reasons to avoid using BETWEEN when comparing two datetimes. Particularly in the example the answer uses. You should be using the various symbols for GREATER THAN and LESS THAN.
...One of the symptoms of an approaching nervous breakdown is the belief that ones work is terribly important.... Bertrand Russell
November 17, 2011 at 12:49 pm
GPO (11/17/2011)
There are good reasons to avoid using BETWEEN when comparing two datetimes. Particularly in the example the answer uses. You should be using the various symbols for GREATER THAN and LESS THAN.
No, I don't think you can (if not reverting to subqueries...)
The "AND" operator will check against the date-columns seperately - not simultaneously which in the case with "BETWEEN".
Using GREATER THAN and LESS THAN in the above question would actually return all the records.
(Sorry, being a bit rusty I'm not entirely sure this is correct, but I've been there, done that and bought the friggin' t-shirt...)
November 17, 2011 at 1:16 pm
Ola L Martins-329921 (11/17/2011)
GPO (11/17/2011)
There are good reasons to avoid using BETWEEN when comparing two datetimes. Particularly in the example the answer uses. You should be using the various symbols for GREATER THAN and LESS THAN.No, I don't think you can (if not reverting to subqueries...)
The "AND" operator will check against the date-columns seperately - not simultaneously which in the case with "BETWEEN".
Using GREATER THAN and LESS THAN in the above question would actually return all the records.
(Sorry, being a bit rusty I'm not entirely sure this is correct, but I've been there, done that and bought the friggin' t-shirt...)
I'm afraid I don't understand you.
WHERE MyDate BETWEEN '20111101' AND '20111130' will not return everything for november - it will return everything for november 1 through 29, plus everything that happened at november 30, exactly midnight.
WHERE MyDate BETWEEN '20111101' AND '20111201' will also not return everything for november - it will return everything for november, plus everything that happened at december 1, exactly midnight.
The only correct code to return everything for november, nothing less, nothing more, is to use seperate >= and < tests:
WHERE MyDate >= '20111101' AND MyDate < '20111201'
November 17, 2011 at 2:58 pm
Ola L Martins-329921 (11/17/2011)
The "AND" operator will check against the date-columns seperately - not simultaneously which in the case with "BETWEEN".Using GREATER THAN and LESS THAN in the above question would actually return all the records.
BETWEEN is completely equivalent to a combination of <= and >=
SomeColumn BETWEEN @Var1 and @Var2 is exactly the same as SomeColumn >= @Var1 and SomeColumn <= @Var2. In fact, the SQL parser converts the BETWEEN into the <= and >= form during parsing (pre-execution)
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
November 17, 2011 at 3:02 pm
GPO (11/17/2011)
There are good reasons to avoid using BETWEEN when comparing two datetimes. Particularly in the example the answer uses. You should be using the various symbols for GREATER THAN and LESS THAN.
I wonder what your opinion is on INTERSECT, EXCEPT, or Common Table Expressions?
One BETWEEN statement easier to read than two or more GREATER THAN and LESS THAN statements.
November 17, 2011 at 3:40 pm
GilaMonster (11/17/2011)
Ola L Martins-329921 (11/17/2011)
The "AND" operator will check against the date-columns seperately - not simultaneously which in the case with "BETWEEN".Using GREATER THAN and LESS THAN in the above question would actually return all the records.
BETWEEN is completely equivalent to a combination of <= and >=
SomeColumn BETWEEN @Var1 and @Var2 is exactly the same as SomeColumn >= @Var1 and SomeColumn <= @Var2. In fact, the SQL parser converts the BETWEEN into the <= and >= form during parsing (pre-execution)
I have seen this myth propagated from time to time (use lt and gt in lieu of between).
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
November 17, 2011 at 6:21 pm
If I were to write the query (which was )
SELECT *
FROM #DateTest
WHERE SampleDate BETWEEN @DATE1 AND @DATE2
I would have written it
SELECT *
FROM #DateTest
WHERE SampleDate >= @DATE1
AND SampleDate < (@DATE2 + 1)
That way I don't get into strife when my data contains a time that is greater than 23:59 and less than the start of the next day. Obviously I would have to remember to sanitise my inputs to ensure that the @date2 value coming in was a whole date (had no time fraction). Sorry I wasn't clearer earlier.
EDIT! EDIT! EDIT! I would have written it this way assuming of course that I wanted to return all the activity on @DATE2.
...One of the symptoms of an approaching nervous breakdown is the belief that ones work is terribly important.... Bertrand Russell
November 17, 2011 at 6:43 pm
The only correct code to return everything for november, nothing less, nothing more, is to use seperate >= and < tests:
WHERE MyDate >= '20111101' AND MyDate < '20111201'
Massive thanks Hugo for putting this much more elegantly than I. Sorry to everyone else:blush:
...One of the symptoms of an approaching nervous breakdown is the belief that ones work is terribly important.... Bertrand Russell
November 18, 2011 at 2:37 am
Hugo Kornelis (11/17/2011)
Ola L Martins-329921 (11/17/2011)
GPO (11/17/2011)
There are good reasons to avoid using BETWEEN when comparing two datetimes. Particularly in the example the answer uses. You should be using the various symbols for GREATER THAN and LESS THAN.No, I don't think you can (if not reverting to subqueries...)
The "AND" operator will check against the date-columns seperately - not simultaneously which in the case with "BETWEEN".
Using GREATER THAN and LESS THAN in the above question would actually return all the records.
(Sorry, being a bit rusty I'm not entirely sure this is correct, but I've been there, done that and bought the friggin' t-shirt...)
I'm afraid I don't understand you.
WHERE MyDate BETWEEN '20111101' AND '20111130' will not return everything for november - it will return everything for november 1 through 29, plus everything that happened at november 30, exactly midnight.
WHERE MyDate BETWEEN '20111101' AND '20111201' will also not return everything for november - it will return everything for november, plus everything that happened at december 1, exactly midnight.
The only correct code to return everything for november, nothing less, nothing more, is to use seperate >= and < tests:
WHERE MyDate >= '20111101' AND MyDate < '20111201'
2 × :blush:
I confused this with another project I was working on a loooong time ago. No wonders I got it mixed up.
Thx Hugo for the explanation. And thx GilaMonster for explaining the parsing-part.
I will now crawl back under the stone where I came from... still blushing...
November 18, 2011 at 2:39 am
GPO (11/17/2011)
I would have written it
SELECT *
FROM #DateTest
WHERE SampleDate >= @DATE1
AND SampleDate < (@DATE2 + 1)
That would have worked for the "old" date/time related data types (datetime and smalldatetime). But for the new date/time datatypes (datetime2, date, datetimeoffset), adding a day by using +1 is not supported. I suggest you switch to using DATEADD(day, 1, @DATE2) instead - that works for all date/tme datatypes, so your code won't break if you one day change to another data type.
November 21, 2011 at 4:35 am
Good question, which illustrates a point that has been known to trap inexperienced people and may help some of them for falling into the trap of thinking that when comparing a date and a datetime the date is implicitly converted to a date rather than the date being implicitly converted to a datetime.
But the description
I am extending Dwayne Dibley's question to show how 'Time' part in date is considered by SQL server
at the front of the question is a little inaccurate, albeit rather amusing. There is no 'Time' part in date, the issue being illustrated is actually what time part will be used in the result of an implicit conversion of a date (which has no time part) to datetime (which has to have one) [or for that matter to datetime2, or smalldatetime - all three datetime types have higher type precedence that date].
Tom
November 21, 2011 at 4:41 am
Thomas Abraham (11/17/2011)
After Che Guevara's question from yesterday, I'll take the easy two points.
Drat! I'll have to change my avatar to something less recognisable! :crying: Either that or change my nickname to "El comandante" so that it matches the avatar. :w00t:
Edit: I changed it. Still unmatched, though.
Tom
Viewing 15 posts - 16 through 30 (of 32 total)
You must be logged in to reply to this topic. Login to reply