1. 程式人生 > >按日期範圍查詢

按日期範圍查詢

後臺開發sql server使用DATEDIFF()函式和GetDate()函數了,取今天、昨天、本週、上週、本月、上月等資料
方法名稱:
DATEDIFF ( datepart , startdate , enddate )
釋義:計算時間差
datepare值:year | quarter | month | week | day | hour | minute | second | millisecond
startdate:開始日期
enddate :結束日期
GetDate():獲取當前的系統日期

例子中表名為tablename ,條件欄位名為time

查詢今天

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

查詢昨天

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

查詢本週

SELECT * FROM tablename where datediff(week,time,getdate())=0

查詢上週

SELECT * FROM tablename where datediff(week,time,getdate())=1

查詢本月

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

查詢上月

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

查詢本季度的

select * from T_InterViewInfo where datediff(QQ,time,getdate())=0