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?