附录,其他日期处理方法
1)去掉时分秒declare @ datetime set @ = getdate() ——‘2003-7-1 10:00:00’SELECT @,DATEADD(day, DATEDIFF(day,0,@), 0)
2)显示星期几select datename(weekday,getdate())
3)如何取得某个月的天数declare @m int set @m=2 ——月份select datediff(day,‘2003-’+cast(@m as varchar)+‘-15’ ,‘2003-’+cast(@m+1 as varchar)+‘-15’)
另外,取得本月天数select datediff(day,cast(month(GetDate()) as varchar)+‘-’+cast(month(GetDate()) as varchar)+‘-15’ ,cast(month(GetDate()) as varchar)+‘-’+cast(month(GetDate())+1 as varchar)+‘-15’)
或者使用计算本月的最后一天的脚本,然后用DAY函数区最后一天SELECT Day(dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())+1, 0)))
4)判断是否闰年:SELECT case day(dateadd(mm, 2, dateadd(ms,-3,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)))) when 28 then ‘平年’ else ‘闰年’ end或者select case datediff(day,datename(year,getdate())+‘-02-01’,dateadd(mm,1,datename(year,getdate())+‘-02-01’))
when 28 then ‘平年’ else ‘闰年’ end 5)一个季度多少天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))
四、示例总结:declare @ datetime set @ = '1995-02-25 11:00:50' —— 1995-01-01 正好是?星期日select @ as 日期,dateadd(year,datediff(year,0,@),0) as 所在年的第一天,dateadd(year,1+datediff(year,0,@),0)-1 as 所在年的最后一天,dateadd(quarter,datediff(quarter,0,@),0) as 所在季的第一天,dateadd(quarter,1+datediff(quarter,0,@),0)-1 as 所在季的最后一天,dateadd(month,datediff(month,0,@),0) as 所在月的第一天,dateadd(month,1+datediff(month,0,@),0)-1 as 所在月的最后一天,dateadd(week,datediff(week,0,@),0) as 所在周的第一天,dateadd(week,1+datediff(week,0,@),0)-1 as 所在周的最后一天