SQL
  Home arrow SQL arrow Page 2 - SQL Date Handling and Data Trends
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  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 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

SQL Date Handling and Data Trends
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-12-13

    Table of Contents:
  • SQL Date Handling and Data Trends
  • Turning the Dates into Integers
  • Modular Arithmetic
  • Changes for SQL Server, Access, and Oracle

  • 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

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    SQL Date Handling and Data Trends - Turning the Dates into Integers


    (Page 2 of 4 )

    Before you can isolate these trends, you must turn the dates into integers to more easily put them into the appropriate buckets. You can pick an arbitrary date and start counting from there. Table 4-2 shows some source data.

    Table 4-2. Page views by date  

      whn

    pages

    2005-06-11

    13368

    2005-06-12

    8129

    2005-06-13

    44043

      …

     

    In Table 4-3, I’ve chosen the first day of the millennium, Monday, January 1, 2001, as day zero. Every date must be converted into the number of days since then. The mechanism for converting to integers is different on different engines. In MySQL, you can create this view using theTO_DAYSfunction:

      CREATE VIEW webalizer2 AS
        SELECT TO_DAYS(whn)-TO_DAYS(DATE '2001-01-01') whn, pages
          FROM webalizer;

    Table 4-3. Converting dates to integers

    whn

    pages

    1622

    13368

    1623

    8129

    1624

    44043

     

    With dates now represented by integers, you can perform arithmetic on them. Taking the modulus 7 value gives you the day of the week. Because 2001-01-01 was a Monday, you will get 0 on every seventh day from then. Tuesday will give you 1, Wednesday 2, and so on, with Sunday having the value 6.

    More SQL Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "SQL Hacks," published by O'Reilly. We hope...
     

    Buy this book now. This article is excerpted from chapter four of the book SQL Hacks, written by Andrew Cumming and Gordon Russell (O'Reilly, 2006; ISBN: 0596527993). Check it out today at your favorite bookstore. Buy this book now.

    SQL ARTICLES

    - Focusing SQL Queries
    - Complex SQL Queries
    - A Close Look at the SQL Query
    - Generating Reports with SQL Date Handling
    - Creating SQL Reports Based on Date Criteria
    - SQL Date Handling and Data Trends
    - Date Handling
    - Introduction to SQL
    - Lies, Damn Lies, Statistics, and SQL







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway