Problem Solution Discussion Performing Date Conversion Using SQL

my m, y = lc 1, 2; use lowercase month name val = sprintf 04d-02d-00, y, map{m} } Aft er st oring t he result ing values int o MySQL, you can ret rieve t hem for display in t he original m ont h year form at by issuing a SELECT st at em ent t hat rewrit es t he dat es using a DATE_FORMAT expression: DATE_FORMATdate_val,bY

10.34 Performing Date Conversion Using SQL

10.34.1 Problem

You want t o convert dat es using SQL st at em ent s.

10.34.2 Solution

For export , use t he DATE_FORMAT funct ion t o rewrit e t he values. For im port , read t he values int o a st ring colum n and convert t hem t o t rue DATE values.

10.34.3 Discussion

Suppose you want t o export dat a from MySQL int o an applicat ion t hat doesnt underst and I SO- form at dat es. One way t o do t his is t o export t he dat a int o a file, leaving t he dat es in I SO form at . Then run t he file t hrough som e kind of ut ilit y like cvt _dat e.pl t hat rewrit es t he dat es int o t he required form at . Anot her approach is t o export t he dat es direct ly in t he required form at by rewrit ing t hem wit h DATE_FORMAT . Suppose you need t o export dat a from a t able, but wit h t he dat es writ t en in U.S. MM-DD-CCYY form at . The following script can accom plish t his. I t t akes t he nam es of a dat abase and t able as it s argum ent s, t hen dum ps t he t able in t ab-delim it ed form at wit h t he dat es in any DATE , DATETIME , or TIMESTAMP colum ns reform at t ed. The script does t his by exam ining t he t able m et adat a t o get t he colum n t ypes, t hen const ruct ing a SELECT st at em ent t hat uses DATE_FORMAT t o rewrit e t he dat es. Ot her colum ns in t he t able are writ t en wit hout change: usrbinperl -w iso_to_us.pl - Export a table with dates rewritten from ISO format CCYY-MM-DD to U.S. format MM-DD-CCYY. This is done by generating a SELECT statement that selects all the columns of the table, but uses DATE_FORMAT to rewrite the dates. Writes each row as a tab-delimited, linefeed-terminated line. use strict; use DBI; ... process command-line options not shown ... ARGV == 2 or die Usage: 0 [options] db_name tbl_name\n; my db_name = shift ARGV; my tbl_name = shift ARGV; ... connect to database not shown ... Read table metadata from MySQL to get colum names and types. Use the types to detect DATE, DATETIME, and TIMESTAMP columns so their contents can be rewritten with DATE_FORMAT . my col; my sth = dbh-prepare SHOW COLUMNS FROM tbl_name; sth-execute ; while my row = sth-fetchrow_array { if row[1] =~ datetime|timestamp { row[0] = DATE_FORMATrow[0], m-d-Y T AS row[0]; } elsif row[1] =~ date { row[0] = DATE_FORMATrow[0], m-d-Y AS row[0]; } push col, row[0]; } my query = SELECT\n\t . join ,\n\t, col . \nFROM tbl_name; Execute SELECT statement and dump out the result sth = dbh-prepare query; sth-execute ; while my val = sth-fetchrow_array { convert NULL undef values to empty strings val = map { defined _ ? _ : } val; print join \t, val . \n; } dbh-disconnect ; exit 0; To see how t his script works, suppose you have t he following t able: CREATE TABLE datetbl i INT, c CHAR10, d DATE, dt DATETIME, ts TIMESTAMP ; The SELECT st at em ent t hat t he script const ruct s t o export t he cont ent s of datetbl looks like t his: SELECT i, c, DATE_FORMATd, m-d-Y AS d, DATE_FORMATdt, m-d-Y T AS dt, DATE_FORMATts, m-d-Y T AS ts FROM datetbl Thus, if datetbl cont ains t he following rows: 3 abc 2001-12-31 2001-12-31 12:05:03 20011231120503 4 xyz 2002-01-31 2002-01-31 12:05:03 20020131120503 The script generat es out put t hat looks like t his: 3 abc 12-31-2001 12-31-2001 12:05:03 12-31-2001 12:05:03 4 xyz 01-31-2002 01-31-2002 12:05:03 01-31-2002 12:05:03 Going in t he ot her direct ion t o im port non- I SO dat es int o MySQL , norm ally you convert t he dat es t o I SO form at first . Ot herwise, you m ust im port t hem as charact er st rings, which reduces t heir usefulness in t em poral cont ext s. However, in som e cases, you can im port non- I SO dat es as st rings, t hen convert t hem t o I SO- form at DATE values aft erward using SQL st at em ent s. Recipe 10.35 shows an exam ple of t his t echnique.

10.34.4 See Also