为此,请使用STR_TO_DATE()。让我们首先创建一个表-
mysql> create table DemoTable ( DueDate varchar(60) ) ;
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('12-AUG-2016'); mysql> insert into DemoTable values('14-AUG-2018'); mysql> insert into DemoTable values('24-AUG-2012'); mysql> insert into DemoTable values('14-AUG-2012');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------------+ | DueDate | +-------------+ | 12-AUG-2016 | | 14-AUG-2018 | | 24-AUG-2012 | | 14-AUG-2012 | +-------------+ 4 rows in set (0.00 sec)
如果日期为VARCHAR格式,则下面是从MySQL中选择记录的查询。STR_TO_DATE()方法用于将字符串转换为日期-
mysql> select *from DemoTable where str_to_date(DueDate,'%d-%M-%Y')=str_to_date('14-AUG-2012','%d-%M-%Y');
这将产生以下输出-
+-------------+ | DueDate | +-------------+ | 14-AUG-2012 | +-------------+ 1 row in set (0.00 sec)