SELECT TOP 366
IDENTITY(INT, 1, 1) AS n
INTO
tempTable
FROM
sys.all_objects
SELECT
REPLACE(CONVERT(VARCHAR(11), DATEADD(DAY, n,getdate()), 106), ' ', '-') AS dateresult,
DATENAME(weekday,DATEADD(DAY, n,getdate())) nameofday
FROM
tempTable
WHERE
DATEPART(weekday, DATEADD(DAY, n,getdate())) = CASE @@DateFirst
WHEN 7 THEN 4
WHEN 6 THEN 5
WHEN 5 THEN 6
WHEN 4 THEN 7
WHEN 3 THEN 1
WHEN 2 THEN 2
ELSE 3
END
DROP TABLE tempTable
the above query will return following output.
(366 row(s) affected)
dateresult nameofday
--------------------- ------------------------------
17-Apr-2013 Wednesday
24-Apr-2013 Wednesday
01-May-2013 Wednesday
08-May-2013 Wednesday
15-May-2013 Wednesday
22-May-2013 Wednesday
29-May-2013 Wednesday
05-Jun-2013 Wednesday
12-Jun-2013 Wednesday
19-Jun-2013 Wednesday
26-Jun-2013 Wednesday
03-Jul-2013 Wednesday
10-Jul-2013 Wednesday
17-Jul-2013 Wednesday
24-Jul-2013 Wednesday
31-Jul-2013 Wednesday
07-Aug-2013 Wednesday
14-Aug-2013 Wednesday
21-Aug-2013 Wednesday
28-Aug-2013 Wednesday
04-Sep-2013 Wednesday
11-Sep-2013 Wednesday
18-Sep-2013 Wednesday
25-Sep-2013 Wednesday
02-Oct-2013 Wednesday
09-Oct-2013 Wednesday
16-Oct-2013 Wednesday
23-Oct-2013 Wednesday
30-Oct-2013 Wednesday
06-Nov-2013 Wednesday
13-Nov-2013 Wednesday
20-Nov-2013 Wednesday
27-Nov-2013 Wednesday
04-Dec-2013 Wednesday
11-Dec-2013 Wednesday
18-Dec-2013 Wednesday
25-Dec-2013 Wednesday
01-Jan-2014 Wednesday
08-Jan-2014 Wednesday
15-Jan-2014 Wednesday
22-Jan-2014 Wednesday
29-Jan-2014 Wednesday
05-Feb-2014 Wednesday
12-Feb-2014 Wednesday
19-Feb-2014 Wednesday
26-Feb-2014 Wednesday
05-Mar-2014 Wednesday
12-Mar-2014 Wednesday
19-Mar-2014 Wednesday
26-Mar-2014 Wednesday
02-Apr-2014 Wednesday
09-Apr-2014 Wednesday
(52 row(s) affected)
so we have got next 52 wednesday after today.
and if you want to get dates of another day of week you will have to make changes in following part of query.
WHEN 7 THEN 4 --it means if its 7th day of week return 4th day. so wednesday
WHEN 6 THEN 5
WHEN 5 THEN 6
WHEN 4 THEN 7
WHEN 3 THEN 1
WHEN 2 THEN 2
ELSE 3
as per required day..
No comments:
Post a Comment