SQL函数:一个日期段日期占另一个时间段日期的比率(使用DateDIFf函数)

  1. CREATE FUNCTION GetDateDuration_Day              
  2. (                 
  3.   @startdate  datetime,               --查询起始日期                  
  4.   @ENDdate   datetime ,               --查询结束日期                  
  5.   @StartDateFee  datetime,            --缴费起始日期                  
  6.   @ENDDateFee   datetime              --缴费结束日期                  
  7. )                
  8. RETURNS decimal(18,3)                
  9. WITH EXECUTE AS CALLER                
  10. AS                
  11. BEGIN                 
  12.                 
  13. declare @DurationTime int ;                   
  14. declare @rate decimal(18,3);                  
  15. IF (@StartDateFee < @ENDDateFee)              
  16. BEGIN              
  17.               
  18. IF ((@ENDdate <= @StartDateFee) OR (@startdate >= @ENDDateFee))                  
  19. BEGIN                  
  20. SET @DurationTime = 0;                  
  21. END                  
  22.                   
  23. IF ((@startdate <= @StartDateFee) AND (@ENDdate <= @ENDDateFee) AND (@StartDateFee <=@ENDdate) )                  
  24. BEGIN                  
  25. SET @DurationTime = DateDIFf(day, @StartDateFee, @ENDdate)                  
  26. END                  
  27.                   
  28. IF ((@startdate <= @StartDateFee) AND (@ENDdate >= @ENDDateFee) AND (@StartDateFee <= @ENDDateFee))                  
  29. BEGIN                  
  30. SET @DurationTime = DateDIFf(day, @StartDateFee, @ENDDateFee)                  
  31.                 
  32. END                  
  33.                   
  34. IF ((@startdate >= @StartDateFee AND @ENDdate<= @ENDDateFee) AND (@startdate <= @ENDdate))                  
  35. BEGIN                  
  36. SET @DurationTime = DateDIFf(day, @startdate, @ENDdate)                   
  37. END                  
  38.                   
  39. IF ((@startdate >= @StartDateFee AND @ENDdate>= @ENDDateFee) AND (@startdate <= @ENDDateFee))                  
  40. BEGIN                  
  41. SET @DurationTime = DateDIFf(day, @startdate, @ENDDateFee)                   
  42. END              
  43.             
  44.  SET @rate = cast(@DurationTime as decimal(18,3))/DateDIFf(day, @StartDateFee, @ENDDateFee);                
  45.  --SET @rate = @DurationTime;              
  46. END                
  47.               
  48. else IF(@StartDateFee >= @ENDDateFee)                
  49. BEGIN                
  50. set @rate = 0;                
  51. END                
  52.                 
  53. return @rate;                
  54. END 
    上面是个小的SQL函数:一个日期段日期占另一个时间段日期的比率  
    于上雷同,下面是月份的计算
  1.  
  2. CREATE FUNCTION  GetDateDuration_Month              
  3. (                 
  4.   @startdate  datetime,                   --查询起始日期                  
  5.   @ENDdate   datetime ,                   --查询结束日期                  
  6.   @StartDateFee  datetime,                --起始日期                  
  7.   @ENDDateFee   datetime                  --结束日期                  
  8. )                
  9. RETURNS decimal(18,3)                
  10. WITH EXECUTE AS CALLER                
  11. AS                
  12. BEGIN                 
  13.                 
  14. declare @DurationTime int ;                   
  15. declare @rate decimal(18,3);                  
  16. IF (@StartDateFee < @ENDDateFee)              
  17. BEGIN              
  18.               
  19. IF ((@ENDdate <= @StartDateFee) OR (@startdate >= @ENDDateFee) )                  
  20. BEGIN                  
  21. SET @DurationTime = 0;                  
  22. END                  
  23.                   
  24. IF ((@startdate <= @StartDateFee) AND (@ENDdate <= @ENDDateFee) AND (@StartDateFee <=@ENDdate) )                  
  25. BEGIN                  
  26. SET @DurationTime = DATEDIFF(Month, @StartDateFee, @ENDdate)                  
  27. END                  
  28.                   
  29. IF ((@startdate <= @StartDateFee) AND (@ENDdate >= @ENDDateFee) AND (@StartDateFee <= @ENDDateFee))                  
  30. BEGIN                  
  31. SET @DurationTime = DATEDIFF(Month, @StartDateFee, @ENDDateFee)                  
  32.                 
  33. END                  
  34.                   
  35. IF ((@startdate >= @StartDateFee AND @ENDdate<= @ENDDateFee) AND (@startdate <= @ENDdate))                  
  36. BEGIN                  
  37. SET @DurationTime = DATEDIFF(Month, @startdate, @ENDdate)                   
  38. END                  
  39.                   
  40. IF ((@startdate >= @StartDateFee AND @ENDdate>= @ENDDateFee) AND (@startdate <= @ENDDateFee))                  
  41. BEGIN                  
  42. SET @DurationTime = DATEDIFF(Month, @startdate, @ENDDateFee)                   
  43. END              
  44.             
  45. SET @rate = cast(@DurationTime as decimal(18,3))/DATEDIFF(Month, @StartDateFee, @ENDDateFee);                       
  46. END                
  47.               
  48.       
  49. else IF(@StartDateFee >= @ENDDateFee)                
  50. BEGIN                
  51. set @rate = 0;                
  52. END                
  53.                 
  54. return @rate;                
  55. END 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值