sql Dateadd()/Datediff()函数利用详情
select dateadd(mm,datediff(mm,0,getdate()),0)----获取当月的第一天
select dateadd(week,datediff(week,0,getdate()),0)----获取本周的星期一
select dateadd(yy,datediff(yy,0,getdate()),0)----获取本年的第一天
select dateadd(qq,datediff(qq,0,getdate()),0)----获取本季度的第一天
select dateadd(dd,datediff(dd,0,getdate()),0)----获取当天的半夜
select dateadd(ms,-3,dateadd(mm,datediff(mm,0,getdate()),0))----上个月的最后一天
select dateadd(ms,-3,dateadd(mm,datediff(m,0,getdate())+1,0))----获取本月的最后一天
select dateadd(ms,-3,dateadd(yy,datediff(yy,0,getdate())+1,0))---获取本年的最后一天
select datename(weekday,getdate())----今天是星期几
Select case day(dateadd(mm,2, dateadd(ms,-3,DATEADD(yy,DATEDIFF(yy,0,getdate()),0))))
when 28
then '平年'
else '闰年'
end -------判断是否闰年
declare @m tinyint,@time smalldatetime
select @m=month(getdate())
select @m=case when @m between 1 and 3 then 1
when @m between 4 and 6
then 4
when @m between 7 and 9
then 7
else 10 end
select
@time=datename(year,getdate())+'-'+convert(varchar(10),@m)+'-01'
select datediff(day,@time,dateadd(mm,3,@time))----判断一个季度有多少天