要在MySQL中重新格式化日期时间,可以使用DATE_FORMAT()。MySQL以yyyy-mm-dd的格式给出。
让我们首先创建一个表-
mysql> create table DemoTable1558 -> ( -> EmployeeJoiningDate datetime -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1558 values(CURDATE()); mysql> insert into DemoTable1558 values(NOW()); mysql> insert into DemoTable1558 values('2018-01-10'); mysql> insert into DemoTable1558 values('2017-12-31');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1558;
这将产生以下输出-
+---------------------+ | EmployeeJoiningDate | +---------------------+ | 2019-10-13 00:00:00 | | 2019-10-13 17:55:33 | | 2018-01-10 00:00:00 | | 2017-12-31 00:00:00 | +---------------------+ 4 rows in set (0.00 sec)
以下是在MySQL中重新格式化日期时间的查询-
mysql> select date_format(EmployeeJoiningDate,'%d-%m-%Y') as changeFormat from DemoTable1558;
这将产生以下输出-
+--------------+ | changeFormat | +--------------+ | 13-10-2019 | | 13-10-2019 | | 10-01-2018 | | 31-12-2017 | +--------------+ 4 rows in set (0.00 sec)