假定日期为varchar数据类型,即不是日期格式。
让我们首先创建一个表。以下是查询-
mysql> create table convertDateDemo -> ( -> AdmissionDate varchar(200) -> );
以下是使用insert命令在表中插入一些记录的查询-
mysql> insert into convertDateDemo values('April 04 2019'); mysql> insert into convertDateDemo values('May 05 2018'); mysql> insert into convertDateDemo values('January 01 2012'); mysql> insert into convertDateDemo values('June 12 2016');
以下是使用select语句显示表中所有记录的查询-
mysql> select * from convertDateDemo;
这将产生以下输出-
+-----------------+ | AdmissionDate | +-----------------+ | April 04 2019 | | May 05 2018 | | January 01 2012 | | June 12 2016 | +-----------------+ 4 rows in set (0.00 sec)
这是使用MySQL转换日期的查询-
mysql> select STR_TO_DATE(AdmissionDate, '%M %e %Y %l:%i%p') AS ConvertDate from convertDateDemo;
这将产生以下输出-
+---------------------+ | ConvertDate | +---------------------+ | 2019-04-04 00:00:00 | | 2018-05-05 00:00:00 | | 2012-01-01 00:00:00 | | 2016-06-12 00:00:00 | +---------------------+ 4 rows in set (0.05 sec)