这是我第一次在CSDN上写博客哈!
--------------------------------查询限定时间内数据小记---------------------------------
//小技巧
select (getdate()-1) AS DiffDate值为:2017-08-20 10:37:51.367
select (getdate()) AS DiffDate值为:2017-08-21 10:37:51.367
结果:减一表示向前平移一天
//总结:weeek 一周之内(星期天--星期六),在本周内则值为0,如果开始时间和结束时间在不同周,
值则根据两周之间的间隔取值,例如上周和下周为1
例如:1.20170820(星期天)---20170826(星期六)
SELECT DATEDIFF(week,'2008-12-20','2008-12-26') AS DiffDate结果:0
2.20170819(星期六)---20170821(星期一)
SELECT DATEDIFF(week,'2008-12-19','2008-12-21') AS DiffDate结果:1
day 一天之内,开始时间和结束时间相隔几天则值为几,与星期和月份无关,取值从0开始
例如:1.同一天
SELECT DATEDIFF(day,'20170821','20170821') AS DiffDate结果:0
2.周一到周二
SELECT DATEDIFF(day,'20170821','20170827') AS DiffDate结果:1
3.周六到周二
SELECT DATEDIFF(day,'20170826','20170829') AS DiffDate结果:3
4.8.31-9.01
SELECT DATEDIFF(day,'20170831','20170901') AS DiffDate结果:1
month 一月之内,开始时间和结束时间为一月之内,值为0,相隔一月为1,以此类推
例如:1.一月之内
SELECT DATEDIFF(MONTH,'20170830','20170831') AS DiffDate结果:0
2.间隔一月
SELECT DATEDIFF(MONTH,'20170831','20170902') AS DiffDate结果:1