I have a string column which acts as a date and I want to select it as a DATE.
DATE
Is it possible?
My sample data format would be:
month/day/year -> 12/31/2011
month/day/year
12/31/2011
As was told at MySQL Using a string column with date text as a date field, you can do
SELECT STR_TO_DATE(yourdatefield, '%m/%d/%Y') FROM yourtable
You can also handle these date strings in WHERE clauses. For example
WHERE
SELECT whatever FROM yourtable WHERE STR_TO_DATE(yourdatefield, '%m/%d/%Y') > CURDATE() - INTERVAL 7 DAY
You can handle all kinds of date/time layouts this way. Please refer to the format specifiers for the DATE_FORMAT() function to see what you can put into the second parameter of STR_TO_DATE().
DATE_FORMAT()
STR_TO_DATE()
1.4m articles
1.4m replys
5 comments
57.0k users