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

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
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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

Searching a column for a specific time, regardless of date, is similar to searching for date-only values. If the column consistently stores just the time portion, then searching the data is simplified. However, unlike date values, the time value is represented by an approximate numeric. So even when the date portion can be ignored, you must still consider rounding errors.

To illustrate time-only searches, consider following table, called TimeSample:

ID  TimeVal
--  -----------------------
1   2002-02-28 10:00:00.000
2   1900-01-01 13:58:32.823
3   1900-01-01 09:59:59.997
4   1900-01-01 10:00:00.000

Here, the TimeVal column is used inconsistently, sometimes storing time only and sometimes storing both date and time. So if you use the following query to retrieve rows with the time 10:00AM:

SELECT * FROM TimeSample
WHERE TimeVal = '10:00:00'

you only get row 4. Row 1 isnít retrieved because the date literal is implicitly cast as a datetime value with a zero date component, which doesnít match the date component of row 1. In addition, row 3 isnít retrieved because this value is close to, but not equal to, 10:00AM.

To ignore the date component of a column, you can code an expression that strips the date/time value of its integer component, such as:

SELECT * FROM TimeSample
WHERE TimeVal - CAST(FLOOR(CAST(TimeVal AS float)) AS datetime) = '10:00'

which returns rows 1 and 4. Unfortunately, thereís no way to accomplish this without using one or more functions. For this reason, itís critical to store time-only data correctly in the first place. If you need to do this kind of search often, you should, if possible, change the database design.

To search for time values that are approximately equal is simply a matter of coding a range of values. If the time-only data are stored consistently without the date component, you can use a query like this:

SELECT * FROM TimeSample
WHERE TimeVal BETWEEN '09:59' AND '10:01'

or

SELECT * FROM TimeSample
WHERE TimeVal > '09:59' AND TimeVal < '10:01'

Both of these queries return rows 3 and 4. Of course, you have to decide what literal values to use for the range of approximation that you prefer.

If the time-only data are stored inconsistently, then you need to accommodate both non-zero date components and a range of time values. For instance, you can use a query like this:

SELECT * FROM TimeSample
WHERE TimeVal - CAST(FLOOR(CAST(TimeVal AS float)) AS datetime) > '09:59'
  AND TimeVal - CAST(FLOOR(CAST(TimeVal AS float)) AS datetime) < '10:01'

which returns rows 1, 3, and 4. Again, though, thereís no way to accomplish this without the use of a function. So you may need to change the database design, if you can.

One other way to approximate time values is to use the smalldatetime data type rather than the datetime data type in the original table. Since smalldatetime always rounds the time portion to the nearest minute, times in the range from 09:59:29.999 to 10:00:29.998 are stored as 10:00. If approximation to the nearest whole minute is sufficient for your application, then using smalldatetime will prevent the need to search for a range of values.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
SQL SERVER ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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