Home arrow SQL arrow Page 4 - Date Handling
SQL

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
TABLE OF CONTENTS:
  1. · Date Handling
  2. · Convert Your Dates
  3. · Parse Dates with Oracle
  4. · Parse Dates with MySQL

print this article
SEARCH DEVARTICLES

Date Handling - Parse Dates with MySQL
(Page 4 of 4 )

MySQL has a similar function, calledSTR_TO_DATE. This works with the format strings in MySQL format:

  INSERT INTO d VALUES (STR_TO_DATE('1 Jun 2006', '%d %b %Y'));

%brepresents the abbreviated month name,%dis the day of the month, and%Yis a four-digit year.

Parse Dates with SQL Server

If your input format is a fixed size (with leading zeros), combine theSUBSTRINGfunction to build the string. Convert a string such as'06/18/2006'into a date:

  INSERT INTO d
   
SELECT SUBSTRING(x,7,4)+'-'+
           SUBSTRING(x,1,2)+'-'+
           SUBSTRING(x,4,2)
      FROM (SELECT '06/18/2006' AS x) y;

Please check back next week for the continuation of this article.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
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

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