替换数据格式使用 MySQL STR_TO_DATE
让我们先创建一个表 −
mysql> create table DemoTable(DueDate varchar(100)); Query OK, 0 rows affected (0.78 sec)
使用插入指令在表中插入一些记录 −
mysql> insert into DemoTable values('10-01-2019 10:19:20'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('12-03-2018 11:00:00'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('02-23-2018 04:20:40'); Query OK, 1 row affected (0.19 sec)
使用选择语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+---------------------+ | DueDate | +---------------------+ | 10-01-2019 10:19:20 | | 12-03-2018 11:00:00 | | 02-23-2018 04:20:40 | +---------------------+ 3 rows in set (0.00 sec)
以下是使用 MySQL STR_TO_DATE 替换数据格式的查询 −
mysql> SELECT STR_TO_DATE(REPLACE(DueDate,"'",''), '%c-%e-%Y %T') from DemoTable;
这将产生以下输出 −
+-----------------------------------------------------+ | STR_TO_DATE(REPLACE(DueDate,"'",''), '%c-%e-%Y %T') | +-----------------------------------------------------+ | 2019-10-01 10:19:20 | | 2018-12-03 11:00:00 | | 2018-02-23 04:20:40 | +-----------------------------------------------------+ 3 rows in set (0.00 sec)
广告