sql server實現
日期部分 縮寫 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 datename(weekday,getdate())
/**//*查詢本年的數據*/
select * from users where year(time)=year(getdate())
/**//*查詢本月的數據,time是表users中代表時間的字段*/
select * from users where month(time)=month(getdate()) and year(time)=year(getdate())
/**//*查詢今天的數據,time 是表中代表時間的字段*/
select * from users where day(time)=day(getdate()) and month(time)=month(getdate()) and year(time)=year(getdate())
/**//*計算那一天是星期一*/
select dateadd(wk,datediff(wk,0,getdate()),0)
/**//*計算那一天是周末*/
select dateadd(wk,datediff(wk,0,getdate()),6)
/**//*查詢本周的數據*/
select * from users where datepart(wk, time)=datepart(wk,getdate()) and datepart(yy, time)=datepart(yy,getdate())
/**//*查詢本日的記錄*/
select * from users where(datediff(dd, time,getdate())=0)
/**//*查詢本月的記錄*/
select