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 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章