SQL
  Home arrow SQL arrow Page 3 - Date Handling
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 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
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

Date Handling
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-12-06

    Table of Contents:
  • Date Handling
  • Convert Your Dates
  • Parse Dates with Oracle
  • Parse Dates with MySQL

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Date Handling - Parse Dates with Oracle


    (Page 3 of 4 )

    Oracle has a neat function calledTO_DATE, which allows you to specify the pattern used in your input string:

      INSERT INTO d VALUES (TO_DATE('1 Jun 2006', 'dd Mon yyyy'))

    You can specify a wide range of formats that include “filler” characters other than a space.

    Using this technique, you could write a simple Perl script, for example. If Perl has read a string such as'1 Jun 2006'into the variable$v, you could generate the SQL as:

      my $sql = "INSERT INTO d VALUES (TO_DATE('$v', 'dd Mon yyyy'))";

    If your dates are coming from an untrusted source, you should still check the pattern to guard against SQL injection attacks:

      if ($v !~ /^\d+ \w\w\w \d\d\d\d$/){
       warn "Injection attack.";
      }

    If you were using XSLT, you might want to use code such as this:

      <stylesheet xmlns="http://www.w3.org/1999/XSL/Transform">
        <template match="foo">
          INSERT INTO dd VALUES (
            TO_DATE('<value-of select='@bar'/>',
                   ,'dd Mon yyyy'))
        </template>
      </stylesheet>

    That sheet would take care of input such as<foo bar='1 Jun 2006'/>.

    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 6 hosted by Hostway