`
rimoer
  • 浏览: 98845 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

求一个月中的全部天数

 
阅读更多

一.建一个可获取当前日期的视图

CREATE VIEW mydate
AS
SELECT mydate = getdate()

二,建立函数,参数为一个1-12的数字表示月份.


create FUNCTION uf_getdays(@newdate varchar(2))
RETURNS int AS
BEGIN
declare @year as int
declare @month as int
declare @nextmonth as int
declare @days as int
declare @date as datetime
select @date= mydate from mydate
if @newdate <> ''
set @date=cast(cast(year(@date) as varchar(4))+'-'+@newdate+'-'+cast(day(@date) as varchar(2)) as datetime)

set @year=cast(year(@date) as int)
set @month=cast(month(@date) as int)

if @month=12
begin
set @year = @year +1
set @month =1
set @nextmonth =@month+1
end
else
begin
set @nextmonth =@month+1
end

set @days=datediff(day,cast(@year as varchar)+'-'+cast(@month as varchar)+'-01',cast(@year as varchar)+'-'+cast(@nextmonth as varchar)+'-01')

return @days
END

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics