Parse date in MySQL

MysqlDate FormatDate FormattingDate Parsing

Mysql Problem Overview


How to convert the following into date for insertion/update into a TIMESTAMP or DATE field in MySQL?

'15-Dec-09'

DATE_FORMAT() is used to format date, but not the other way around.

Mysql Solutions


Solution 1 - Mysql

You may want to use the STR_TO_DATE() function. It's the inverse of the DATE_FORMAT() function.

> STR_TO_DATE(str,format) > > This is the inverse of the DATE_FORMAT() function. It takes a string str and a format string format. STR_TO_DATE() returns a DATETIME value if the format string contains both date and time parts, or a DATE or TIME value if the string contains only date or time parts. If the date, time, or datetime value extracted from str is illegal, STR_TO_DATE() returns NULL and produces a warning.

Example:

SELECT STR_TO_DATE('15-Dec-09', '%d-%b-%y') AS date;
+------------+
| date       |
+------------+
| 2009-12-15 |
+------------+
1 row in set (0.00 sec)

Solution 2 - Mysql

Here's a table of format %-codes used in DATE_FORMAT() and STR_TO_DATE().

     -----examples-for------
     1999-12-31   2000-01-02
     23:59:58.999 03:04:05      identical to
     ------------ ----------    -------------
 %a  Fri          Sun      LEFT(DAYNAME(d),3)
 %b  Dec          Jan      LEFT(MONTHNAME(d),3)
 %c  12           1             MONTH(d)            
 %D  31st         2nd           DAYOFMONTH(d)+st,nd,rd
 %d  31           02       LPAD(DAYOFMONTH(d),0,2)
 %e  31           2             DAYOFMONTH(d)     
 %f  999000       000000   LPAD(MICROSECOND(t),6,0)
 %H  23           03       LPAD(HOUR(t),2,0)
 %h  11           03
 %I  11           03
 %i  59           04       LPAD(MINUTE(t),2,0)
 %j  365          002
 %k  23           3             HOUR(t)             
 %l  11           3
 %M  December     January       MONTHNAME(d)  
 %m  12           01       LPAD(MONTH(d),2,0) 
 %p  PM           AM
 %r  11:59:58 PM  03:04:05 AM
 %S  58           05       LPAD(SECOND(t),2,0)
 %s  58           05       LPAD(SECOND(t),2,0)
 %T  23:59:58     03:04:05
 %U  52           01       LPAD(WEEK(d,0),2,0)
 %u  52           00       LPAD(WEEK(d,1),2,0)
 %V  52           01      RIGHT(YEARWEEK(d,2),2)
 %v  52           52      RIGHT(YEARWEEK(d,3),2)
 %W  Friday       Sunday        DAYNAME(d)
 %w  5            0             DAYOFWEEK(d)-1
 %X  1999         2000     LEFT(YEARWEEK(d,2),4)
 %x  1999         1999     LEFT(YEARWEEK(d,3),4)
 %Y  1999         2000          YEAR(d)   
 %y  99           00      RIGHT(YEAR(d),2)
 %%  %            %

or

%X%V 199952       200001        YEARWEEK(d,2)
%x%v 199952       199952        YEARWEEK(d,3)

By the way there are no %-codes for unpadded minutes or seconds:

     59           4             MINUTE(t)  
     58           5             SECOND(t)

In action:

 '15-Dec-09' == DATE_FORMAT('2009-12-15', '%d-%b-%y')
 '2009-12-15' == STR_TO_DATE('15-Dec-09', '%d-%b-%y')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser157195View Question on Stackoverflow
Solution 1 - MysqlDaniel VassalloView Answer on Stackoverflow
Solution 2 - MysqlBob SteinView Answer on Stackoverflow