Possible Duplicate:
Get dates from a week number in T-SQL
How do I get the date value if I have a week number in SQL Query.
Like if I pass 26, it should give me 06/24/2012. If I pass 27, I should get 07/01/2012
Any help will be appreciated :)
Sots
Possible Duplicate:
Get dates from a week number in T-SQL
How do I get the date value if I have a week number in SQL Query.
Like if I pass 26, it should give me 06/24/2012. If I pass 27, I should get 07/01/2012
Any help will be appreciated :)
Sots
In SQL Server
DECLARE @StartDate DATE, @WeekVal INT
SET @WeekVal = 26 -- Set the week number
SET @StartDate = DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) -- Start of current year
;WITH cte AS (
SELECT @StartDate AS DateVal, DATEPART(wk, @StartDate) AS WeekVal, 1 AS RowVal
UNION ALL
SELECT DATEADD(d, 1, DateVal), DATEPART(wk, DATEADD(d, 1, DateVal)), RowVal + 1
FROM cte WHERE RowVal < 365
)
SELECT MIN(DateVal) StartOfWeek
FROM cte
WHERE WeekVal = @WeekVal
OPTION (MAXRECURSION 365);
If this doesn't work, try using WEEK() instead of WEEKOFYEAR().
CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY + INTERVAL (WEEKNO - WEEKOFYEAR(CURDATE())) WEEK
This gives you the start and end dates of the week. [For SQL Server]
Declare @week integer set @week = 26
Declare @Year Integer Set @Year = year(getdate())
declare @date datetime
-- ------------------------------------
Set @date = DateAdd(day, 0,
DateAdd(month, 0,
DateAdd(Year, @Year-1900, 0)))
set @date = Dateadd(week, @week-1, @date)
select @date startweek, DATEADD (D, -1 * DatePart (DW, @date) + 7, @date) endweek
This was the result from it:
startweek endweek
----------------------- -----------------------
2012-07-01 00:00:00.000 2012-07-07 00:00:00.000
(1 row(s) affected)