格式化MySQL日期并转换为年-月-日
让我们首先创建一个表-
mysql> create table DemoTable666(AdmissionDate varchar(200));
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable666 values('Sat,20 Jul 2019 04:29:35'); mysql> insert into DemoTable666 values('Fri,02 Oct 2018 12:19:15'); mysql> insert into DemoTable666 values('Sun,01 Aug 2016 11:10:05'); mysql> insert into DemoTable666 values('Fri, 06 Nov 2015 04:06:05 -0500');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable666;
这将产生以下输出-
+---------------------------------+ | AdmissionDate | +---------------------------------+ | Sat,20 Jul 2019 04:29:35 | | Fri,02 Oct 2018 12:19:15 | | Sun,01 Aug 2016 11:10:05 | | Fri, 06 Nov 2015 04:06:05 -0500 | +---------------------------------+ 4 rows in set (0.00 sec)
以下是将上述日期格式转换为年月日的查询-
mysql> select DATE_FORMAT(STR_TO_DATE(AdmissionDate, '%a, %d %b %Y %H:%i:%S'),'%Y-%m-%d %H:%i:%s') AS MyDate from DemoTable666;
这将产生以下输出-
+---------------------+ | MyDate | +---------------------+ | 2019-07-20 04:29:35 | | 2018-10-02 12:19:15 | | 2016-08-01 11:10:05 | | 2015-11-06 04:06:05 | +---------------------+ 4 rows in set, 1 warning (0.00 sec)