如何在 MySQL 中更新日期格式?
我们首先创建一个表 −
mysql> create table DemoTable630 (ArrivalDate varchar(100)); Query OK, 0 rows affected (0.55 sec)
使用插入命令在表中插入一些记录 −
mysql> insert into DemoTable630 values('2015-21-01'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable630 values('2018-25-12'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable630 values('2019-15-07'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable630 values('2016-31-03'); Query OK, 1 row affected (0.17 sec)
使用选择语句显示表中的所有记录 −
mysql> select *from DemoTable630;
这将生成以下输出 −
+-------------+ | ArrivalDate | +-------------+ | 2015-21-01 | | 2018-25-12 | | 2019-15-07 | | 2016-31-03 | +-------------+ 4 rows in set (0.00 sec)
以下是用于转换日期格式的查询 −
mysql> select str_to_date(ArrivalDate,'%Y-%d-%m') from DemoTable630;
这将生成以下输出 −
+-------------------------------------+ | str_to_date(ArrivalDate,'%Y-%d-%m') | +-------------------------------------+ | 2015-01-21 | | 2018-12-25 | | 2019-07-15 | | 2016-03-31 | +-------------------------------------+ 4 rows in set (0.00 sec)
广告