Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Calendar showing fridays and corresponding dates

Author  Topic 

nietzky
Yak Posting Veteran

75 Posts

Posted - 2015-01-06 : 13:19:29
I need to generate a calendar showing all Fridays of the month for past year and up to 2020. Thank YOU
DATE DAY YEAR
9/12/2014 0:00 Friday 2014
9/19/2014 0:00 Friday 2014
9/26/2014 0:00 Friday 2014
10/3/2014 0:00 Friday 2014
10/10/2014 0:00 Friday 2014
10/17/2014 0:00 Friday 2014
10/24/2014 0:00 Friday 2014
10/31/2014 0:00 Friday 2014
11/7/2014 0:00 Friday 2014
12/12/2014 0:00 Friday 2014
12/19/2014 0:00 Friday 2014
12/26/2014 0:00 Friday 2014
1/2/2015 0:00 Friday 2015
1/9/2015 0:00 Friday 2015
:
:
How would I do that? If it is easier to do all days that is fine.... ex. Monday, Tuesda....

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-06 : 15:11:43
Here's one way using a tally table:


with n1(n) as (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) v(n)),
n2(n) as (select 1 from n1, n1 _),
n4(n) as (select 1 from n2, n2 _),
n8(n) as (select 1 from n4, n4 _),
N as (select top ((2020-2014)*365) n = ROW_NUMBER() over(order by (select 1)) from n8)

select cast(current_timestamp+n as date) as FridayDate from N
where datepart(dw, dateadd(day, n, current_timestamp)) = 6
order by FridayDate

Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2015-01-06 : 15:26:36
Here's another, slightly smarter:


-- Find day number of Friday of this week
declare @FridayOffset tinyint = (
select n from (values (1),(2),(3),(4),(5),(6),(7)) v(n)
where datepart(dw, current_timestamp + n) = 6
);

with n1(n) as (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1)) v(n)),
n2(n) as (select 1 from n1, n1 _),
n4(n) as (select 1 from n2, n2 _),
n8(n) as (select 1 from n4, n4 _),
N as (select top ((2020-2014)*365/7)
n = @FridayOffset + 7*(-1 + ROW_NUMBER() over(order by (select 1))) -- one Friday per week
from n8)

select cast(current_timestamp+n as date) as FridayDate from N
order by FridayDate;
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2015-01-06 : 17:01:39
Yet another take. Just fill in the starting and ending dates.

DECLARE @starting_date datetime
DECLARE @ending_date datetime
--NOTE: don't need to worry if starting date specified isn't a Friday,
-- the code will adjust forward to the first Friday.
SET @starting_date = '20140101'
SET @ending_date = '20201231'

------------------------------------------------------------------------------------------------------------------------

;WITH
cteTally10 AS (
SELECT 0 AS tally UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0
),
cteTally1000 AS (
SELECT ROW_NUMBER() OVER(ORDER BY c1.tally) - 1 AS week#
FROM cteTally10 c1
CROSS JOIN cteTally10 c2
CROSS JOIN cteTally10 c3
)
SELECT DATEADD(DAY, weeks.week# * 7, first_Friday) AS date
FROM (
--adjust the starting date to the first Fri on or after the starting date
SELECT DATEADD(DAY, -DATEDIFF(DAY, 4, max_possible_starting_date) % 7, max_possible_starting_date) AS first_Friday
FROM (
SELECT DATEADD(DAY, 6, @starting_date) AS max_possible_starting_date
) AS max_possible_starting_date
) AS first_Friday
INNER JOIN cteTally1000 weeks ON
DATEADD(DAY, weeks.week# * 7, first_Friday) <= @ending_date
ORDER BY date

Go to Top of Page

nietzky
Yak Posting Veteran

75 Posts

Posted - 2015-02-04 : 12:08:35
Thank you all.
Go to Top of Page
   

- Advertisement -