1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
-- SQLSERVER 查询今天、昨天、本周、上周、本月、上月数据
-- 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
-- 查询本年
select * from Keywords where datediff(year, Addtime,getdate())=0
|