要获取字符串作为日期,请使用STR_TO_DATE()方法。让我们首先创建一个表-
mysql> create table DemoTable1445 -> ( -> AdmissionDate varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1445 values('01.10.2019'); mysql> insert into DemoTable1445 values('31.12.2018'); mysql> insert into DemoTable1445 values('01.02.2017');
使用选择显示表中的所有记录-
mysql> select * from DemoTable1445;
这将产生以下输出-
+---------------+ | AdmissionDate | +---------------+ | 01.10.2019 | | 31.12.2018 | | 01.02.2017 | +---------------+ 3 rows in set (0.00 sec)
以下是在MySQL中获取字符串作为日期的查询-
mysql> update DemoTable1445 set AdmissionDate=str_to_date(AdmissionDate,'%.%.%'); Rows matched: 3 Changed: 3 Warnings: 0
让我们再次检查表记录-
mysql> select * from DemoTable1445;
这将产生以下输出-
+---------------+ | AdmissionDate | +---------------+ | 2019-10-01 | | 2018-12-31 | | 2017-02-01 | +---------------+ 3 rows in set (0.00 sec)