Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql - How to convert date from ISO 8601 format and save to mysql column?

Tags:

datetime

mysql

I've a column in a table (varchar) with dates in this format 2013-09-05T10:10:02Z

How do I convert this into datetime format and save it in another column, using an update query?

like image 251
user187809 Avatar asked Oct 14 '13 09:10

user187809


People also ask

How do I format a date column in MySQL?

MySQL retrieves and displays DATE values in ' YYYY-MM-DD ' format. The supported range is '1000-01-01' to '9999-12-31' . The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format.

How do I convert a date to a string in MySQL?

In SQL Server, you can use CONVERT function to convert a DATETIME value to a string with the specified format. In MySQL, you can use DATE_FORMAT function.


1 Answers

You can use the STR_TO_DATE function:

UPDATE table1 SET col2 = STR_TO_DATE(col1,'%Y-%m-%dT%TZ')

Example:

mysql> select STR_TO_DATE('2013-09-05T10:10:02Z','%Y-%m-%dT%TZ');
+----------------------------------------------------+
| STR_TO_DATE('2013-09-05T10:10:02Z','%Y-%m-%dT%TZ') |
+----------------------------------------------------+
| 2013-09-05 10:10:02                                |
+----------------------------------------------------+
1 row in set (0.00 sec)
like image 171
Hari Menon Avatar answered Oct 10 '22 23:10

Hari Menon