2015-10-07 47 views
0

我困在存储过程中,我需要一些帮助。我有一个程序,告诉我在两个日期内有多少个月,每周或每天的日期,但我不知道它是如何计算两天之间的日差作为'2015-06-01'和' 2015-06-30'为此它显示它是整月或入境是1个月,但'2015-06-01'和'2015-07-01'它显示1个月和2天。此外,它通过将它们的数字除以7来计算剩余的几天,但我想要日历周。我无法做到这两件事。需要日历天的存储过程帮助

这是代码 -

CREATE PROCEDURE GetDateParts 
(
@StartDate DATE , 
@EndDate DATE 
) 
AS 
BEGIN 

    /* variables to be used */ 
    DECLARE @Return VARCHAR(5) 
    /* 
     Get the difference between the two dates 
     add 1 to the value to include the first day in the count 
    */ 
    , @TotalNumberOfDays INT 
    , @DaysInMonth TINYINT; 

    /* table variable to store the number of days in a month 
     this would be better as a fixed SQL table as it'll 
     be called a lot */ 
    DECLARE @Months TABLE 
     ([Month] TINYINT, [NoDays] TINYINT); 

    /* month values */ 
    INSERT INTO @Months 
    VALUES 
     (1, 31), 
     (2, 28), 
     (3, 31), 
     (4, 30), 
     (5, 31), 
     (6, 30), 
     (7, 31), 
     (8, 31), 
     (9, 30), 
     (10, 31), 
     (11, 30), 
     (12, 31); 

    /* Create Result table */ 
    DECLARE @ResultTable TABLE ([MonthNumber] TINYINT, [FullMonth] BIT, [Weeks] TINYINT, [Days] TINYINT) 

    -- set the count as the mointh number 
    DECLARE @Count TINYINT = MONTH(@StartDate); 
    SET @TotalNumberOfDays = DATEDIFF(day, @StartDate, @EndDate)+1 
    WHILE @Count <= MONTH(@EndDate) 
    BEGIN 

     /* get the number of days in the month */ 
     SELECT @DaysInMonth = [NoDays] FROM @Months WHERE [Month] = @Count; 

     /* 
     Check if it's a leap year and alter the number of days in Febuary to 29 
     This was taken from https://www.mssqltips.com/sqlservertip/1527/sql-server-function-to-determine-a-leap-year/ 
     */ 
     IF((SELECT CASE DATEPART(mm, DATEADD(dd, 1, CAST((CAST(@StartDate AS VARCHAR(4)) + '0228') AS DATE))) 
       WHEN 2 THEN 1 
       ELSE 0 
       END) = 1) AND MONTH(@StartDate) = 2 
      SET @DaysInMonth = 29; 

     IF (@TotalNumberOfDays >= @DaysInMonth) 
     BEGIN 
      INSERT INTO @ResultTable ([MonthNumber], [FullMonth]) 
      VALUES (@Count, 1) 

      SET @TotalNumberOfDays = @TotalNumberOfDays - (@DaysInMonth-DAY(@StartDate)); 

      SET @StartDate = DATEADD(day, (@DaysInMonth-DAY(@StartDate)+1), @StartDate); 

      SET @Count = @Count + 1; 
     END 
     ELSE IF (@TotalNumberOfDays >= 7) 
     BEGIN 
      INSERT INTO @ResultTable ([MonthNumber], [Weeks]) 
      VALUES (@Count, CAST(@TotalNumberOfDays/7 AS INT)) 
      DECLARE @Remainder TINYINT = @TotalNumberOfDays%7; 

      IF (@Remainder = 0) 
      BEGIN 
       SET @Count = @Count + 1; 
      END 
      ELSE 
      BEGIN 
       SET @TotalNumberOfDays = @Remainder; 
      END 
     END 
     ELSE 
     BEGIN 
      INSERT INTO @ResultTable ([MonthNumber], [Days]) 
      VALUES (@Count, @TotalNumberOfDays) 
      SET @Count = @Count + 1; 
     END 

    END; 

    -- Return Results 
    SELECT * FROM @ResultTable; 
END 

谁能帮助,因为它是对我很重要。 Thnaks提前

+0

可能只是使用函数'datediff(wk,@start,@end)','datediff(dd,@start,@end)'? –

+0

我真的不能使用它,因为它显示了一周的差异,即使有4天的日期差异。我只想显示一周有7天的差异,并且有整整一周,因为我有数据明智地表示不在分手天数 –

+0

然后使用'datediff(dd,@start,@end)/ 7'作为周。 –

回答

0

寻找你的代码有需要的外部循环为月的嵌套循环(这将循环通过开始和结束日期之间的月份)和内部WHILE循环将迭代本月的周数也会检查本周是否已满,第一天是周开始日期。我们假设星期一的日期。如果不满足此条件,则将表格填入DayWise Entry。

为任何查询进一步评论您的文本。

+0

是啊那就是我想要的......你能帮我解决吗? –

+0

你能帮助我的男人! –