--方法一
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