Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Search

XML Feeds

Google Ads

« How Are Dates Stored In SQL Server?Converting Columns To Date From Datetime Does Not Result In A Scan In SQL Server 2008 »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

Do you use between to return data that has dates? Do you know that between will get everything since midnight from the first criteria and up to midnight exactly from the second criteria. If you do BETWEEN 2006-10-01 AND 2006-10-02 then all the values that are greater or equal than 2006-10-01 and less or equal to 2006-10-02 will be returned. So no values after 2006-10-02 midnight will be returned.

Let's test this out, first let's create this table

  1. CREATE TABLE SomeDates (DateColumn DATETIME)

Insert 2 values

  1. INSERT INTO SomeDates VALUES('2006-10-02 00:00:00.000')
  2. INSERT INTO SomeDates VALUES('2006-10-01 00:00:00.000')

Return everything between '2006-10-01' and '2006-10-02'

  1. SELECT *
  2. FROM SomeDates
  3. WHERE DateColumn BETWEEN '20061001' AND '20061002'
  4. ORDER BY DateColumn

This works without a problem

Let's add some more dates including the time portion

  1. INSERT INTO SomeDates VALUES('2006-10-02 00:01:00.000')
  2. INSERT INTO SomeDates VALUES('2006-10-02 00:00:59.000')
  3. INSERT INTO SomeDates VALUES('2006-10-02 00:00:01.000')
  4. INSERT INTO SomeDates VALUES('2006-10-01 00:01:00.000')
  5. INSERT INTO SomeDates VALUES('2006-10-01 00:12:00.000')
  6. INSERT INTO SomeDates VALUES('2006-10-01 23:00:00.000')

Return everything between '2006-10-01' and '2006-10-02'

  1. SELECT *
  2. FROM SomeDates
  3. WHERE DateColumn BETWEEN '20061001' AND '20061002'
  4. ORDER BY DateColumn

Here is where it goes wrong; for 2006-10-02 only the midnight value is returned the other ones are ignored

Now if we change 2006-10-02 to 2006-10-03 we get what we want

  1. SELECT *
  2. FROM SomeDates
  3. WHERE DateColumn BETWEEN '20061001' AND '20061003'
  4. ORDER BY DateColumn

Now insert a value for 2006-10-03 (midnight)

  1. INSERT INTO SomeDates VALUES('2006-10-03 00:00:00.000')

Run the query again

  1. SELECT *
  2. FROM SomeDates
  3. WHERE DateColumn BETWEEN '20061001' AND '20061003'
  4. ORDER BY DateColumn

We get back 2006-10-03 00:00:00.000, between will return the date if it is exactly midnight

If you use gt;= and lt; then you get exactly what you need

  1. SELECT *
  2. FROM SomeDates
  3. WHERE DateColumn >= '20061001' AND DateColumn < '20061003'
  4. ORDER BY DateColumn
  1. --Clean up
  2. DROP TABLE SomeDates

So be careful when using between because you might get back rows that you did not expect to get back and it might mess up your reporting if you do counts or sums

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
1923 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

1 comment

Comment from: Naomi [Member] Email
Can you please expand this blog to include time intervals only?
11/18/09 @ 09:16

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)