sql语句:日期函数相关的SQL总结datediff(),datepart()
本主题由 tip 创建于 2009-9-28 16:08:23计算时间的函数中有,年(yy),季(q),月(m),周(ww),时期(w),天(d),时(hh),分(n),秒(s)
getdate():返回当前日期
获取当前月份的天数:
select datediff(d,getdate(),dateadd(m,1,getdate()))
获取当前年,季,月,周,天,时,分,秒:
select datepart(yy,getdate()) //年
select datepart(q,getdate()) //季
select datepart(m,getdate()) //月
select datepart(ww,getdate())//周
select datepart(w,getdate()) //星期,从0开始.
select datepart(d,getdate()) //天
select datepart(hh,getdate())//时
select datepart(n,getdate()) //分
select datepart(s,getdate()) //秒
获取当前天的前后五天日期:
select dateadd(dd,5,getdate())
select dateadd(dd,-5,getdate())
获取当前日期与目标日期相差的年,月,周,日,时,分,秒:
select datediff(yy,'2005-01-01 14:12:01',getdate())
select datediff(q,'2005-01-01 14:12:01',getdate())
select datediff(m,'2005-01-01 14:12:01',getdate())
select datediff(ww,'2005-01-01 14:12:01',getdate())
select datediff(d,'2005-01-01 14:12:01',getdate())
select datediff(hh,'2005-01-01 14:12:01',getdate())
select datediff(n,'2005-01-01 14:12:01',getdate())
select datediff(s,'2005-01-01 14:12:01',getdate())
获取当前月份添加的信息:
select count(*) as num, Origin from ks_article month(getdate())=DatePart("m",adddate) group by Origin
放心注册,GeeKaa不会虚耗你半点时间,你只会发现更多乐趣。 | 立即注册 |