求一个月中的全部天数

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值