CREATE FUNCTION GetMonthDays(@yourDate varchar(10)) --取本月天数 时间格式 / yyyyMM
RETURNS int
AS
BEGIN
declare @getDate datetime,@getDateYear int,@getDateMonth int,@MonthLastDate datetime,@NextMonthYear int,@NextMonthMonth int,@returnDateDay int
if len(@yourDate)=6
begin
set @yourDate=left(@yourDate,4)+'-'+right(@yourDate,2)+'-01'
end
set @getDate=cast(@yourDate as datetime)
set @getDateYear=year(@getDate) --传入日期年份
set @getDateMonth=month(@getDate) --传入日期月份
if @getDateMonth=12
begin
set @NextMonthYear=@getDateYear+1
set @NextMonthMonth=1
end
else
begin
set @NextMonthYear=@getDateYear
set @NextMonthMonth=@getDateMonth+1
end
set @MonthLastDate=cast(str(@NextMonthYear)+'-'+str(@NextMonthMonth)+'-01' as datetime)-1 --本月最后一天
set @returnDateDay=day(@MonthLastDate) --本月天数
return @returnDateDay
END