如何获得当月的天数?

类别:数据库 点击:0 评论:0 推荐:
--方法一
DECLARE @month int, @year int
SELECT @month = 6, @year = 2004
SELECT DATEPART(dd,DATEADD(m,1,CAST(STR(@month) + '/01/' + STR(@year) as
datetime)) - 1)
--方法二
DECLARE @DATE DATETIME
SET @DATE = GETDATE()

SELECT CAST(
 -- First day of next month
 DATEADD(MONTH,DATEDIFF(MONTH,0,@DATE)+1,0)
 -
 -- First day of current month
 DATEADD(MONTH,DATEDIFF(MONTH,0,@DATE),0)
 AS INT )

--方法三
这个你也可以写一个UDF来实现:
create function fn_NumOfDaysInMon (@date datetime)
 returns int
as
begin
declare @begmonth datetime,@endmonth datetime,@days int
select @begmonth=dateadd(month,datediff(month,0,@date),0)
select @endmonth=dateadd(dd,-datepart(dd,@date),dateadd(mm,1,@date))
select @days=datediff(dd,@begmonth,@endmonth)+1
return @days
end

本文地址:http://com.8s8s.com/it/it20631.htm