2013-09-27 2 views
2

두 날짜 사이의 주중 시작일과 종료일을 반환하는 쿼리 작성 방법은 무엇입니까?반환 할 SQL Server 쿼리 일 주 시작, 종료 날짜

예 :

declare 
    @startDate date = '2013-07-01' 
    , @endDate date = '2013-09-30' 

SELECT WeekData(@startDate, @endDate) 

결과 :

Name, Week1Start, Week1End, Week2Start, Week2End, Week3Start, Week3End, Week4Start, Week4End, Week5Start, Week5End 
--------------------------------------------------------------------------------------------------------------------------------------------------- 
'July 2013', '2013-07-01', '2013-07-05', '2013-07-08', '2013-07-12', '2013-07-15', '2013-07-19', '2013-07-22', '2013-07-26', '2013-07-29', '2013-07-31' 
'August 2013', '2013-08-01', '2013-08-02', '2013-08-05', '2013-08-09', '2013-08-12', '2013-08-16', '2013-08-19', '2013-08-23', '2013-08-26', '2013-08-30' 
'September 2013', '2013-09-02', '2013-09-06', '2013-09-09', '2013-09-13', '2013-09-16', '2013-09-20', '2013-09-23', '2013-08-27', '2013-09-30', '2013-09-30' 

업무와 주 월요일 또는 매월 1 일에 시작하여 금요일 또는 해당 월의 마지막 날에 끝납니다.

+1

중복 가능성을 [무엇을 얻을 수있는 가장 좋은 방법입니다 시작 날짜와 SQL에서 두 날짜 사이의 주말의 끝 날짜] (http://stackoverflow.com/questions/12752136/what-is-the-best-way-to-get-start-date-and-end-date -of-the-week-between-two-date) –

답변

2

출력이 정확한 형식이어야합니까? 주어진 범위에서 일치하는 날짜의 전체 목록을 원한다면 DATEPART (또는 이전 답변 에서처럼 DATENAME)를 참조하여 일종의 T-SQL 루핑을 사용하는 것이 좋습니다.

DECLARE @startDate DATETIME = '7/1/2013', @endDate DATETIME = '9/30/2013'; 

DECLARE @currentDate DATETIME = @startDate; 
DECLARE @currentMonth INT, @currentDay INT, @currentDayOfWeek INT; 
DECLARE @prevMonth INT = 0, @prevDay INT = 0; 

WHILE @currentDate <= @endDate 
BEGIN 
    SET @currentMonth = MONTH(@currentDate); 
    SET @currentDay = DAY(@currentDate); 

    -- see if we need a month header 
    IF(@currentMonth <> @prevMonth) 
    BEGIN 
     PRINT 'Month: ' + CAST(@currentMonth AS VARCHAR) + '/' + CAST(YEAR(@currentDate) AS VARCHAR); 
     SET @prevMonth = @currentMonth; 
    END 

    -- see if it is a week start or end 
    SET @currentDayOfWeek = DATEPART(WEEKDAY, @currentDate); 
    IF((@currentDay = 1) 
     OR (@currentDay = 31) -- this would obviously need to be more sophisticated 
     OR (@currentDayOfWeek = 2) -- Monday 
     OR (@currentDayOfWeek = 6) -- Friday 
    ) PRINT @currentDate; 

    -- go on to the next date 
    SET @currentDate = @currentDate + 1; 
END 
0

추측이 조금 늦습니다. 여기에 내가 (마지막으로 주 토의 시작을 할 일을 가정) 작업 주간의 지난 2 년을 생성하는 데 사용 내용은 다음과 같습니다

Declare @workWeeks Table (
    weekStart datetime, 
    weekEnd datetime, 
    weekText varchar(25) 
) 

Declare @count int, 
     @lastSunday datetime, 
     @nextSaturday datetime 
Select @count = 0, 
     @lastSunday = DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE())/7 * 7, -1), 
     @nextSaturday = DATEADD(MS, -3, DATEADD(DAY, DATEDIFF(DAY, -1, GETDATE())/7 * 7, 6)) -- Next Sunday minus 3 ms 

While @count < 52 
Begin 
    Insert Into @workWeeks Values(
     DATEADD(WK, [email protected], @lastSunday), 
     DATEADD(WK, [email protected], @nextSaturday), 
     CONVERT(varchar, DATEADD(WK, [email protected], @lastSunday), 101) + ' - ' + CONVERT(varchar, DATEADD(WK, [email protected], @nextSaturday), 101)) 
    Set @count = @count + 1 
End 

Select * From @workWeeks 

을 그리고 당신은 다시이 얻을 것이다 :

weekStart    weekEnd     weekText 
----------------------- ----------------------- ------------------------- 
2015-06-14 00:00:00.000 2015-06-20 23:59:59.997 06/14/2015 - 06/20/2015 
2015-06-07 00:00:00.000 2015-06-13 23:59:59.997 06/07/2015 - 06/13/2015 
2015-05-31 00:00:00.000 2015-06-06 23:59:59.997 05/31/2015 - 06/06/2015 
2015-05-24 00:00:00.000 2015-05-30 23:59:59.997 05/24/2015 - 05/30/2015 
... 

이 예에서 @lastSunday 및 @nextSaturday, -1 및 6을 설정하는 데 사용 된 표현식의 끝에서 요일 조정 값으로 원숭이를 작성하여 요일의 시작일과 종료일을 변경할 수 있습니다.

하지만 원래 질문을 올바르게 읽는다면 금주의 금을 원할 것입니다.

-- For Mon thru Fri: 
Insert Into @workWeeks Values(
    DATEADD(WK, [email protected], DATEADD(DD, 1, @lastSunday)), 
    DATEADD(WK, [email protected], DATEADD(DD, -1, @nextSaturday)), 
    CONVERT(varchar, DATEADD(WK, [email protected], DATEADD(DD, 1, @lastSunday)), 101) + ' - ' + CONVERT(varchar, DATEADD(WK, [email protected], DATEADD(DD, -1, @nextSaturday)), 101)) 

을 그리고 당신이 다시 얻을 것이다 :이 경우 너무 같은 삽입 문에서 각 방향으로 일일에 의해 조정의

weekStart    weekEnd     weekText 
----------------------- ----------------------- ------------------------- 
2015-06-15 00:00:00.000 2015-06-19 23:59:59.997 06/15/2015 - 06/19/2015 
2015-06-08 00:00:00.000 2015-06-12 23:59:59.997 06/08/2015 - 06/12/2015 
2015-06-01 00:00:00.000 2015-06-05 23:59:59.997 06/01/2015 - 06/05/2015 
2015-05-25 00:00:00.000 2015-05-29 23:59:59.997 05/25/2015 - 05/29/2015 
... 
관련 문제