如何在MySQL中将char字段转换为datetime字段?
让我们首先创建一个表。在这里,我们以char类型声明了日期-
mysql> create table DemoTable1472 -> ( -> ShippingDate char(35) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1472 values('12/31/2017 10:50'); mysql> insert into DemoTable1472 values('01/10/2018 12:00'); mysql> insert into DemoTable1472 values('03/20/2019 09:30');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1472;
这将产生以下输出-
+------------------+ | ShippingDate | +------------------+ | 12/31/2017 10:50 | | 01/10/2018 12:00 | | 03/20/2019 09:30 | +------------------+ 3 rows in set (0.00 sec)
以下是在MySQL中将char字段转换为datetime字段的查询-
mysql> select str_to_date(replace(ShippingDate,'/',','),'%,%,% %T') from DemoTable1472;
这将产生以下输出-
+----------------------------------------------------------+ | str_to_date(replace(ShippingDate,'/',','),'%m,%d,%Y %T') | +----------------------------------------------------------+ | 2017-12-31 10:50:00 | | 2018-01-10 12:00:00 | | 2019-03-20 09:30:00 | +----------------------------------------------------------+ 3 rows in set (0.00 sec)