Get start date and end date of a week of the month

2

Good afternoon, guys.

Today I have a problem getting the start date and end date for a week of the month. Let's break it down:

I have a combo that brings me the weeks of a month posted. To know how many weeks I have in January 2015, for example, I use the following SQL:

SELECT theLast - theFirst + 1 AS Semanas
FROM (
    SELECT 1 + Number AS theMonth,
    DATEDIFF(DAY, -1, DATEADD(MONTH, Number, DATEADD(YEAR, 2015 - 1900, 0))) / 7 AS theFirst,
    DATEDIFF(DAY, -1, DATEADD(MONTH, Number, DATEADD(YEAR, 2015 - 1900, 30))) / 7 AS theLast
    FROM master..spt_values
    WHERE Type = 'P' AND Number < 12
) AS d 
WHERE d.theMonth = 01

Until this point okay, very quiet. Returned 5 weeks. The problem is finding the beginning and the end of a week. For example, if I post week = 03 and month = 01, it should show me '2015-01-11' as initial and '2015-01-17' as final.

Does anyone have any idea how to do this?

    
asked by anonymous 20.07.2015 / 20:14

2 answers

3

In the

20.07.2015 / 21:33
0

The following command takes the first Monday of the month:

SELECT @PrimeiraSegundaFeira = DATEADD(WEEK, DATEDIFF(WEEK, 0,
    DATEADD(DAY, 6 – DATEPART(DAY, GETDATE()), GETDATE())), 0)

The Monday of the third week can be obtained as follows:

SELECT @TerceiraSegundaFeira = DATEADD(WEEK, 2, @PrimeiraSegundaFeira)

The Friday of the third week can be obtained as follows:

SELECT @TerceiraSextaFeira = DATEADD(DAY, 5, @TerceiraSegundaFeira)
    
20.07.2015 / 20:33