Home arrow SQL arrow Page 3 - Date Handling

Date Handling

SQL can handle many date calculations. This four-part series will show you how to do date calculations against your database and get reports based on specific periods of time. This article, the first in the series, is excerpted from chapter four of the book SQL Hacks, written by Andrew Cumming and Gordon Russell (O'Reilly, 2006; ISBN: 0596527993). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 13
December 06, 2007
  1. · Date Handling
  2. · Convert Your Dates
  3. · Parse Dates with Oracle
  4. · Parse Dates with MySQL

print this article

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">
        TO_DATE('<value-of select='@bar'/>',
               ,'dd Mon yyyy'))

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

blog comments powered by Disqus

- 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

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 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials