SQL Server
  Home arrow SQL Server arrow Page 3 - How to Search for Date and Time Values Usi...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
SQL SERVER

How to Search for Date and Time Values Using SQL Server 2000
By: Bryan Syverson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 108
    2003-12-04

    Table of Contents:
  • How to Search for Date and Time Values Using SQL Server 2000
  • How Dates and Times are Stored in SQL Server
  • Date/Time Values are Approximate Numerics
  • Dates Without Times and Times Without Dates
  • The Effect of Database Design on Querying
  • Performance Considerations in Querying
  • How to Search by Date
  • How to Search by Time

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    How to Search for Date and Time Values Using SQL Server 2000 - Date/Time Values are Approximate Numerics


    (Page 3 of 8 )

    Datetime and smalldatetime are like the floating-point data types, float and real, in that they’re approximate numerics. That means the value retrieved from SQL Server may be different from the value that was originally stored. For example, if you store the expression 10/3.0 in a column of data type float, you’ll retrieve a value 3.3333330000000001. Although this is a reasonable representation of ten thirds, it’s not exact since it’s rounded past the 6th digit. In fact, if you add three such values together, you get 9.9999990000000007, not 10. Of course, most programmers understand this as a rounding error. And it’s a persistent problem for all digital computers, not just those running SQL Server. Still, you need to be aware of it as you code search conditions.

    In contrast, when working with exact numeric data, the value retrieved from SQL Server is exactly the value that was originally stored. For example, if you store 10/3.0 in a column of data type int, it’s stored as 3 and retrieved as 3. In this case, SQL Server implicitly casts the result of the expression as a real value, 3.333333. Then, SQL Server implicitly casts 3.333333 as an integer because it’s being stored in a column of type int. Although this is still a rounding error, it occurs before the value is stored, not as a result of the physical limitations of computer storage. In other words, the error was introduced by using the wrong data type, not by the inherent limitation of the data type itself. Since the system always returns the same value as was stored, the data type is exact.

    Now, to see how this affects date/time values, consider the date and time value for 8:00AM on January 4, 1900. As you saw above, noon on this day is stored as 3.5, or halfway through the fourth day. In contrast, 8:00AM is one third of the way through the day, so its representation will be approximate. To see this for yourself, submit the following query:

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

    You’ll get the following result:

    3.3333333333333335

    But if you submit this query:

    SELECT CAST(3.3333333 AS datetime), CAST(3.3333334 AS datetime)

    you’ll get the following results:

    1900-01-04 07:59:59.997             1900-01-04 08:00:00.003

    As you can see, these three values are all quite close. In fact, they’re close enough to be considered 8:00AM for most applications. However, in a search condition based on a single value, such as:

    WHERE (DTValue = '1900-01-04 08:00')

    you’d only match those rows where the stored value exactly matches 3.3333333333333335. You’ll see how to get around this later in this article.

    More SQL Server Articles
    More By Bryan Syverson


       · This article provides me the understanding of storeing date values in Sql server but...
     

    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







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek