热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> 数据库>>正文内容

SQL查询月、天、周、年

发布时间:2008年12月29日点击数: 停留的风

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 * from users where (DATEDIFF(mm, time, GETDATE()) = 0)

/**//*查询本年的记录*/
select * from users where (DATEDIFF(yy, time, GETDATE()) = 0)

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作