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

How to Search by Time

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.

Published with the express written permission of the copyright holder. Copyright 200

]]>

Leave a comment

Your email address will not be published.