Home arrow SQL Server arrow Page 7 - How to Search for Date and Time Values Using SQL Server 2000

How to Search for Date and Time Values Using SQL Server 2000

SQL ServerBefore you can effectively query date/time (or temporal) data, you have to know something about how date/time values are stored.  In this article, Bryan demonstrates the various ways in which to query an SQL Server to successfully retrieve the records you may be seeking.

Author Info:
By: Bryan Syverson
Rating: 4 stars4 stars4 stars4 stars4 stars / 122
December 04, 2003
  1. · How to Search for Date and Time Values Using SQL Server 2000
  2. · How Dates and Times are Stored in SQL Server
  3. · Date/Time Values are Approximate Numerics
  4. · Dates Without Times and Times Without Dates
  5. · The Effect of Database Design on Querying
  6. · Performance Considerations in Querying
  7. · How to Search by Date
  8. · How to Search by Time

print this article

How to Search for Date and Time Values Using SQL Server 2000 - How to Search by Date
(Page 7 of 8 )

Frequently. youíll need to search a date/time column for a specific date, regardless of time. If the data in the column have been used consistently with the time component set to zero, thatís no problem. You just search for the date youíre looking for.

But consider the following table, called DateSample:

ID  DateVal
--  -----------------------
1   2001-02-28 10:00:00.000
2   2002-02-28 13:58:32.823
3   2002-02-29 00:00:00.000
4   2002-02-28 00:00:00.000

As you can see, the DateVal column is used inconsistently. The third and fourth values indicate that the column might have been intended to store dates only, but the first two values indicate that this wasnít enforced.

As a result, if you use the following query to retrieve rows with the date February 28, 2002:

SELECT * FROM DateSample
WHERE DateVal = '2002-02-28'

the result set includes only row 4 instead of both rows 2 and 4. Thatís because the date literal is implicitly cast as a datetime value which, in this case, has a zero time component. Since this doesnít exactly match the value in row 2, that row isnít returned.

How can you get around the time component? If the query is run often, you should base the search on a range of values, as in:

SELECT * FROM DateSample
WHERE DateVal BETWEEN '2002-02-28' AND '2002-02-28 23:59:59.997'

Remember that the BETWEEN clause retrieves values that are equal to the upper and lower limits, so you canít code the upper limit as just '2002-02-29'. If you do, then youíll incorrectly retrieve row 3. Another way to get the same result is to use comparison operators:

SELECT * FROM DateSample
WHERE DateVal >= '2002-02-28' AND DateVal < '2002-02-29'

If the query is run infrequently (to produce a report only once a month, for instance), you can code an expression in the WHERE clause that strips the date/time value of its fractional component. For example, this query:

SELECT * FROM DateSample
WHERE CAST(FLOOR(CAST(DateVal AS float)) AS datetime) = '2002-02-28'

returns both rows 2 and 4. In addition, there are many other expressions that you can use to accomplish this same result (my SQL book, Murachís SQL for SQL Server, covers a couple of others).

By the way, if you wished to retrieve rows with the day February 28, regardless of year, you could code the following query:

SELECT * FROM DateSample
WHERE MONTH(DateVal) = 2 AND DAY(DateVal) = 28

which retrieves rows 1, 2, and 4. Since there isnít a way to accomplish this without using one or more functions, however, this query shouldnít be run frequently against a production database. If you need to perform this kind of search on a query that runs often, you should change the design of the database, if possible. Then, you can create a separate, indexed column to store the portion of the date/time value that you need to search.

blog comments powered by Disqus

- Executing SQL Server Stored Procedure from P...
- How to Search for Date and Time Values Using...
- Replication: SQL Server 2000 - Part 2
- Replication: SQL Server 2000 - Part 1
- SQL Sever: Storing Code in Binary or Text Fi...
- Execute SQL on Multiple Tables/Columns - New...
- How to Connect to a SQL Server from Visual F...
- SQL Server Hardware Tuning and Performance M...
- Primary Key on Multiple Tables Ė New RDBMS C...
- Migrating from Sybase to SQL Server
- What's Best for DBAs? GUI or T-SQL Comma...
- How to Perform a SQL Server Performance Audit
- An Introduction To The Bulk Copy Utility
- SQL Server Stored Procedures 101
- Building Your First SQL Server 2000 Database

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials