sql 日期查询

来源:百度知道 编辑:UC知道 时间:2024/06/20 17:31:12
在表Table1中有一个日期型字段Bdate(生日),欲查询在98年1月1日到2003年10月1日出生的记录,我用SQL语句为:select * from table1 where bdate between '1998-01-01' and '2003-10-1',如果我想采用Year、Month和Day函数,则怎么样写该语句呢?

这还要看你数据库这个字段是什么类型了.如果是字符类型的.要用到LEFT、RIGHT、SUBSTRING函数。如果是日期型的,就可以用到YEAR、MONTH、DAY函数。

select * from table1 where
(year(bdate)>='1998' and month(bdate)>='1' and day(bdate)>='1') and (year(bdate)<='2003' and month(bdate)<='10' and day(bdate)<='1')

为什么要用函数呢?这样也可以做到