Monday, October 8, 2007

MySQL get date from string

Let's say you have a string, like '18-Sep-2007' and you want to format it in date format %Y-%m-%d. You can use STR_TO_DATE function. First parameter is your string, and the second parameter is the format of your string, in this case %d-%b-%Y. You can see the format values here:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format

So STR_TO_DATE('18-Sep-2007','%d-%b-%Y') = 2007-09-18

You can use this result to compare dates, to order dates, or other date operations.

No comments: