Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Nakul Vachhrajani is a Technical Specialist & Systems development professional with iGATE. He holds a MCTS (SQL Server 2008: Implementation & Maintenance)
Browse by Tags · View All
#SQLServer 242
SQL Server 241
Administration 208
DBA 197
Tips 186
Development 186
T-SQL 181
#TSQL 179
Guidance 123
Tools and Utilities 112

Archive · View All
April 2011 14
March 2012 11
December 2011 11
March 2011 11
December 2012 10
October 2011 10
January 2011 10
January 2013 9
November 2012 9
October 2012 9

Date Comparison – Dissecting BETWEEN - Use BETWEEN or comparison (greater than/less than) operators with equal to?

Sep 12 2011 12:00AM by Nakul Vachhrajani   

Whenever we talk about inclusive date comparison & filtering in T-SQL, the only two options that come to mind are the BETWEEN clause and the greater than (>) and less than (<) operators paired with the equal to operator. Today, let us attempt to find out if there is a difference between the two.

Let me mention that our study today will be restricted to dates only. DATETIME values need extra “precautions” on the part of the query developer to cover the time portion in the comparison. However, most legacy systems (and as we would see from the examples in the AdventureWorks2008R2 database), use separate storage for date and time values and therefore, the need to filter on the date value is a very realistic user scenario.

Dissecting BETWEEN

Without spending much time, let’s get right along to the simple test. Let’s assume that once a year, the Adventure Works Bicycle Company prepares a list of all employees who were hired in the organization 10 years ago, (because we are in 2011, this would be in 2001) to send out an organization-wide recognition of service notification. Quite simply, the query would be something like:

SELECT * FROM HumanResources.Employee
WHERE HireDate BETWEEN '2001-01-01' AND '2001-12-31'
GO

We get 2 records in return:

image

Now, let us study the actual execution plan to find out what goes on under the covers.

image

As it turns out, BETWEEN is internally implemented by pairing a set of comparison operators with the equal to operator!

Conclusion

We can safely conclude that BETWEEN is just a facade for the comparison operators. The only difference is in the fact that comparison operators, by themselves offer exclusive comparison (i.e. the comparison excludes the value being compared to). For an inclusive comparison, they need to be paired with the equal to (=) operator – something which the BETWEEN does out-of-the-box.

So is BETWEEN really required?

I studied the Books-On-Line pages for BETWEEN and the Comparison operators, and could not find any difference other than the one mentioned above (about BETWEEN being inclusive). So, the big question now becomes whether BETWEEN is really required? Wouldn’t our code be much better if we avoid the need for Microsoft SQL Server to transform the between into comparison operators?

Well, in my humble opinion, that’s where the issue is. We, as developers and ultimately, humans, tend to make mistakes. The compilers warn us of syntactical issues with our code, but there is nothing that would warn us about a logical bug. A single miss in placing an equal to operator (or placing of an operator in excess), might result in code that would be logically incorrect. Using BETWEEN when we need inclusive comparison just helps in avoiding such errors.

So, BETWEEN is required – it’s not mandatory, but recommended. And that’s why it’s a T-SQL coding best practice.

Interested in more such surprises?

If you are interested in more such surprises, please do check out my post on the differences between CAST & CONVERT here: http://beyondrelational.com/blogs/nakul/archive/2011/07/18/cast-v-s-convert-is-there-a-difference-as-far-as-sql-server-is-concerned-which-is-better.aspx.

Until we meet next time,

Be courteous. Drive responsibly.

Tags: 


Nakul Vachhrajani
4 · 33% · 10680
9
 
0
Lifesaver
 
 
0
Learned
 
0
Incorrect



Submit

9  Comments  

  • Good post. But note that COL BETWEEN @a and @b is more readable and understandable than COL>=@a and COL<=@b. It is becuase BETWEEN is plain english and easy to understand. Also you dont need to repeat the column twice for the comaprison. Also if any of the operators <,= or > is missed or interpreted wrongly, the result is not accurate.

    But I prefer using >= and <=. It is becuase it is very useful where compare datetime values. You need to specify time part when you use BETWEEN.

    commented on Sep 14 2011 8:25AM
    Madhivanan
    3 · 39% · 12472
  • Exactly my point, Madhivanan when I attempt to answer the question whether BETWEEN is really required.

    Thank-you for your valuable comments. It is good to know that I am not the only one who gets nervous around people using comparison operators without reviewing their code and ultimately resulting in logical bugs in the application.

    commented on Sep 16 2011 11:15PM
    Nakul Vachhrajani
    4 · 33% · 10680
  • Here are more good examples of why you should neve, EVER use BETWEEN when searching for date ranges... and some GOOD ALTERNATIVES with examples.

    http://garybhaley.com/ComparingDatesInSQL.pdf

    TheSQLWhisperer

    commented on Sep 30 2011 8:03AM
    SQL Whisperer
    2119 · 0% · 6
  • @SQL WHisperer: As I mentioned, the time component will need additional precautions on the part of the developer - there is absolutely no doubt about that. The white paper you point out points out the issues that might occur in detail. It's a good one, and thank-you for sharing.

    However, in most legacy systems, the date and time components are stored separately (and apparently, the need to do still exists because the new DATE type was just introduced). In such cases, the BETWEEN is a much safer alternate.

    BETWEEN has it's uses, and therefore, like almost everything on this planet needs to be used with proper judgement and care.

    commented on Sep 30 2011 8:13AM
    Nakul Vachhrajani
    4 · 33% · 10680
  • For my testing I found converting the target column to DATE for time insensitive comparison performed over 55k rows actually performed better than using full DateTime with filter values inclusive of the time needed, and I find both to be accurate. I did get a good laugh about the idea of comparison based on converting the date to a string...

    SELECT * 
    FROM HumanResources.Employee
    WHERE convert(date, HireDate) BETWEEN '2001-01-01' AND '2001-12-31';
    

    Verses

    SELECT * 
    FROM HumanResources.Employee
    WHERE HireDate BETWEEN '2001-01-01 00:00:00.000' AND '2001-12-31 23:59:59.997';
    
    commented on Sep 30 2011 5:14PM
    Dennis Allen
    329 · 0% · 128
  • Just a heads-up... converting dates to strings, and then comparing the strings uses a a LOT of resources, especially if enough queries are pounding your database and cycling through your cache quickly. Take advantage of your execution plan by NOT comparing strings. Again, take a minute and read this article. It's based on many lessons learned, and full of good (free) info.

    http://garybhaley.com/ComparingDatesInSQL.pdf

    TheSQLWhisperer

    commented on Sep 30 2011 7:22PM
    SQL Whisperer
    2119 · 0% · 6
  • @SQL Whisperer: I agree. Conversion to strings is a bad idea here.

    In fact, unless absolutely necessary, I try never to convert to strings for the same reason - it takes a lot of resources.

    commented on Oct 1 2011 8:05AM
    Nakul Vachhrajani
    4 · 33% · 10680
  • Hi guys, I have seen freshers assuming that comparison value order does not matter, but that is not the case. Always smaller value should be first followed by larger value This confusion arises due to English meaning of between. Agree that this situation will not come with an experienced person.

    To avoid this, best way is if we can write the code using >= and <=

    Thanks Jeetendra

    commented on Jun 24 2012 8:42AM
    Jeetendra
    153 · 1% · 315
  • FYI:The current ANSI syntax is:

    ::= [NOT] BETWEEN [ASYMMETRIC | SYMMETRIC] AND

    The ASYMMETRIC option is the default and it is the curent behavior. The SYMMETRIC opton swaps the two s so it tests "in both directions" at once. ACCESS had this as a dialect and we considered it at one time in ANSI X3H2

    commented on Jun 24 2012 11:43AM
    jcelko
    445 · 0% · 87

Your Comment


Sign Up or Login to post a comment.

"Date Comparison – Dissecting BETWEEN - Use BETWEEN or comparison (greater than/less than) operators with equal to?" rated 5 out of 5 by 9 readers
Date Comparison – Dissecting BETWEEN - Use BETWEEN or comparison (greater than/less than) operators with equal to? , 5.0 out of 5 based on 9 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]