将MySQL日期格式从yyyy-mm-ddThh:mm:ss.sssZ转换为yyyy-mm-dd hh:mm:ss?
首先创建一个表 –
mysql> create table DemoTable668(JoiningDate varchar(200));
Query OK, 0 rows affected (0.97 sec)
使用insert命令在表中插入一些记录。我们将日期格式化为 yyyy-mm-ddThh:mm:ss.sssZ -
mysql> insert into DemoTable668 values('2001-01-10T06:20:00.000Z');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable668 values('2019-07-20T04:00:00.000Z');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable668 values('2016-02-12T05:10:50.000Z');
Query OK, 1 row affected (0.17 sec)
使用select语句显示表中的所有记录 –
mysql> select *from DemoTable668;
将输出以下内容 –
+--------------------------+
| JoiningDate |
+--------------------------+
| 2001-01-10T06:20:00.000Z |
| 2019-07-20T04:00:00.000Z |
| 2016-02-12T05:10:50.000Z |
+--------------------------+
3 rows in set (0.00 sec)
以下是将日期格式从yyyy-mm-ddThh:mm:ss.sssZ转换为yyyy-mm-dd hh:mm:ss的查询 –
mysql> update DemoTable668
SET JoiningDate = DATE_FORMAT(STR_TO_DATE(JoiningDate,'%Y-%m-%dT%H:%i:%s.000Z'),'%Y-%m-%d %H:%i:%s');
Query OK, 3 rows affected (0.11 sec)
Rows matched: 3 Changed: 3 Warnings: 0
让我们再次检查表记录 –
mysql> select *from DemoTable668;
将输出以下内容 –
+---------------------+
| JoiningDate |
+---------------------+
| 2001-01-10 06:20:00 |
| 2019-07-20 04:00:00 |
| 2016-02-12 05:10:50 |
+---------------------+
3 rows in set (0.00 sec)
阅读更多:MySQL 教程