SQLTeam.com | Weblogs | Forums

SQL find date within the week basing on day of the week


#1

Hi

I need to find the date basing on the Day of the week. Below will be the input

1 Sun
2 Mon
3 Tue
4 Wed
5 Thu
6 Fri
7 Sat.

so if I pass 1 (which is Sun), I should get 6/14/2015

like wise if I pass 7(which is Sat), I should get 6/20/2015.

Thanks in Advance
MOhan


#2
DECLARE @N INT = 1;
SELECT DATEADD(dd,-DATEDIFF(dd,0,GETDATE())%7-2+@N,CAST(GETDATE() AS DATE));

#3

Thank you James ! It works perfectly.