Home arrow SQL Server arrow Page 2 - 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 Dates and Times are Stored in SQL Server
(Page 2 of 8 )

Before you can effectively query date/time (or temporal) data, you have to know something about how date/time values are stored. SQL Server supports two date/time data types: datetime and smalldatetime. The difference between the two is the amount of storage used. Datetime uses 8 bytes of storage, while smalldatetime uses only 4 bytes. For this reason, datetime can represent date/time values within a wider range and with more precision than smalldatetime. These differences are summarized in the table below.

TypeMinimumMaximumPrecision
datetimeJan 1, 1753
midnight
Dec 31, 9999
23:59:59.997
(0.003 seconds until midnight)
To the nearest
3.33 milliseconds
smalldatetimeJan 1, 1900
midnight
Jun 6, 2079
23:59
(1 minute until midnight)
To the nearest
minute

Both datetime and smalldatetime represent the date and time as a value thatís equal to the number of days in relationship to a base date. In SQL Server, that base date is midnight on January 1, 1900. As you can see in the table, the smalldatetime type can only represent dates from this base date on. In contrast, the datetime type can also represent dates that are before January 1, 1900. To do that, it stores those values as negative numbers.

To visualize how date/time values are stored, you can think of them as consisting of two parts. The integer portion represents the number of whole days since January 1, 1900. The fractional portion represents the fraction of a day thatís passed since midnight. For example, the date/time value representing noon on January 4, 1900 is stored as 3.5. In this case, 3 represents three full days since the base date and 0.5 represents one half of a day between midnight and noon. To see this, submit the following query:

SELECT CAST(CAST('1900-01-04 12:00' AS datetime) AS float)


 Note: The CAST function explicitly changes the data type of a value as specified. So in this statement, the inner CAST changes the string literal '1900-01-04 12:00' to a value of data type datetime. Then, the outer CAST changes that datetime value to a value of data type float. The final result is a floating-point representation of the datetime value that represents noon on January 4, 1900.

So far, so good. But the problems that crop up in querying date/time data are caused by confusion over two fundamental facts that arenít so obvious. First, date/time data types are approximate numerics, not exact numerics. Second, date/time data types canít store a date without a time or a time without a date.


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