datediff函式
日期部分 縮寫
year y, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
hour hh
minute mi, n
second ss, s
millisecond ms
查詢本日的記錄
select count(*) from tablename where (datediff(dd, thedate, getdate()) = 0)
查詢本月的記錄
select count(*) from tablename where (datediff(mm, thedate, getdate()) = 0)
查詢本年的記錄
select count(*) from tablename where (datediff(yy, thedate, getdate()) = 0)
datepart函式
日期部分 縮寫
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
hour hh
minute mi, n
second ss, s
millisecond ms
查詢本月的記錄
select * from tablename where datepart(mm, thedate) = datepart(mm, getdate()) and datepart(yy, thedate) = datepart(yy, getdate())
查詢本週的記錄
select * from tablename where datepart(wk, thedate) = datepart(wk, getdate()) and datepart(yy, thedate) = datepart(yy, getdate())
查詢本季的記錄
select * from tablename where datepart(qq, thedate) = datepart(qq, getdate()) and datepart(yy, thedate) = datepart(yy, getdate())
其中:getdate()是獲得系統時間的函式。
sql中按日期查詢
查詢當天 1 select from shoporder where datediff day,ordtime,getdate 1 0 查詢當天 2 select from info where datediff dd,datetime,getdate 0 前30天 select from a wh...
sql的日期查詢
1 dw,weekday 表示某個星期的第幾天 ltrim rtrim title 清除title的左右空格 查詢同一天的 select from loginlog where datediff day,logintime,2007 12 28 0 查詢同一星期的 第一種 select from l...
SQL日期查詢
查詢本日的記錄 select from tablename where datepart dd,thedate datepart dd,getdate and datepart mm,thedate datepart mm,getdate and datepart yy,thedate datepa...