SQL Server中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
转载自 https://blog.csdn.net/Candy_Sir/article/details/85231400