借助sql日期函数,可以实现许多我们想要的功能,下面就将为您介绍使用sql日期函数获得一个月中的天数的方法,供您参考,希望对您学习sql日期函数能有所启迪。
--获取给定年份和月份的天数
- alter function fn_getDayByYearMonth(@year int,@month int)
- returns int
- begin
- declare @date datetime
- declare @day int
- if(@month<>12)
- begin
- set @month = @month + 1
- set @date = cast(cast(@year as varchar)+'-'+cast(@month as varchar)+'-1' as datetime)
- end
- if(@month=12)
- begin
- set @date = cast(cast(@year as varchar)+'-'+cast(@month as varchar)+'-31' as datetime)
- end
- set @dayday = day(@date-1)
- return @day
- end
--获取给定年份和月份中***的那一天
- alter function fn_getMaxDate(@year int,@month int)
- returns datetime
- begin
- declare @date datetime
- declare @day int
- set @day = dbo.fn_getDayByYearMonth(@year,@month)
- set @date = cast(cast(@year as varchar)+'-'+cast(@month as varchar)+'-'+cast(@day as varchar) as datetime)
- return @date
- end
【编辑推荐】