要将日期从“ dd / mm / yyyy”转换为“ yyyymmdd”,可以使用date_format()方法。让我们首先创建一个表-
create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Admissiondate varchar(200) );
使用插入命令在表中插入一些记录-
insert into DemoTable(Admissiondate) values('21/10/2014'); insert into DemoTable(Admissiondate) values('01/12/2016'); insert into DemoTable(Admissiondate) values('31/01/2017');
以下是使用select语句显示表中所有记录的查询-
select *from DemoTable;
这将产生以下输出-
+----+---------------+ | Id | Admissiondate | +----+---------------+ | 1 | 21/10/2014 | | 2 | 01/12/2016 | | 3 | 31/01/2017 | +----+---------------+ 3 rows in set (0.00 sec)
以下是将日期从'dd / mm / yyyy'转换为'yyyymmdd'的查询-
select date_format(str_to_date(Admissiondate,'%d/%m/%Y'),'%Y%m%d') from DemoTable;
这将产生以下输出-
+-------------------------------------------------------------+ | date_format(str_to_date(Admissiondate,'%d/%m/%Y'),'%Y%m%d') | +-------------------------------------------------------------+ | 20141021 | | 20161201 | | 20170131 | +-------------------------------------------------------------+ 3 rows in set (0.00 sec)