How to Search for Date and Time Values Using SQL Server 2000
Before 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.
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:
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'
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.