使用sql日期函数获得一个月中的天数


  本文标签:sql日期函数

  借助sql日期函数,可以实现许多我们想要的功能,下面就将为您介绍使用sql日期函数获得一个月中的天数的方法,供您参考,希望对您学习sql日期函数能有所启迪  。

  --获取给定年份和月份的天数

  1. alter function fn_getDayByYearMonth(@year int,@month int)  
  2. returns int  
  3. begin  
  4. declare @date datetime  
  5. declare @day int  
  6. if(@month<>12)  
  7. begin  
  8. set @month = @month + 1  
  9. set @date = cast(cast(@year as varchar)+-+cast(@month as varchar)+-1 as datetime)  
  10. end  
  11. if(@month=12)  
  12. begin  
  13. set @date = cast(cast(@year as varchar)+-+cast(@month as varchar)+-31 as datetime)  
  14. end   
  15. set @dayday = day(@date-1)  
  16. return @day  
  17. end  

  --获取给定年份和月份中最大的那一天

  1. alter function fn_getMaxDate(@year int,@month int)  
  2. returns datetime  
  3. begin  
  4. declare @date datetime  
  5. declare @day int  
  6. set @day = dbo.fn_getDayByYearMonth(@year,@month)  
  7. set @date = cast(cast(@year as varchar)+-+cast(@month as varchar)+-+cast(@day as varchar) as datetime)  
  8. return @date  
  9. end