需要日历天存储过程的帮助

时间:2015-10-07 10:31:15

标签: sql-server sql-server-2008 tsql stored-procedures

我遇到了需要帮助的存储过程。我有一个程序,它告诉我在两个日期内有多少个月或一周或一天,但我不知道如何计算两天之间的日差作为'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

任何人都可以提供帮助,因为这对我很重要。事先提醒

1 个答案:

答案 0 :(得分:0)

期待您的代码需要嵌套循环,其中Outer While循环用于月份(将在开始日期和结束日期之间迭代几个月)和内部WHILE循环将迭代月份中的周数,这也将检查周已满,第一天为周开始日期。让我们说星期一的日期。如果不满足此条件,则将表填入DayWise Entry。

进一步评论您的文字是否有任何疑问。