Convert text format to date in mySQL

Viewed 21

I have a CSV file I imported into mySQL. One of the columns has dates in it but the data type for it in mySQL is 'text'.

How can I convert it to a date format? Example dates in the column:

2/2/2012
7/23/2011
11/27/2011

I've tried using

SELECT STR_TO_DATE(latest_launch,'%m,%d,%Y')
FROM cars;

But the output gives me a blank column. Thanks for the help.

1 Answers

I suggest looking at the 'CAST()' function,

Your solution might look something like this:

    SELECT CAST(<text> AS date);

Where is the text you want to make into the date type.

If you want to change an entire column try something like this:

    SELECT CAST(<column_name> AS DATE)
    FROM Table 

Where <column_name> is the entire column you are trying to convert to the date type.

I hope this was helpful! Let me know if you have any questions!

Related