SQL语句中DATEDIFF()函数的用法

DATEDIFF ( datepart , startdate , enddate )释义:计算时间差;
datepare值:year | quarter | month | week | day | hour | minute | second | millisecond
startdate:开始日期
enddate :结束日期
GetDate():获取当前的系统日期

下面例子中表名为tablename,条件字段名为inputdate

查询今天

SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=0

查询昨天

SELECT * FROM tablename where DATEDIFF(day,inputdate,GETDATE())=1

查询本周

SELECT * FROM tablename where DATEDIFF(week,inputdate,GETDATE())=0

查询上周

SELECT * FROM tablename where DATEDIFF(week,inputdate,GETDATE())=1

查询本月

SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=0

查询上月

SELECT * FROM tablename where DATEDIFF(month,inputdate,GETDATE())=1


版权声明:本文为Candy_Sir原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。