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. 
    
        
            
                
                    
                        
                            
                                | 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 YOUDATE	DAY	YEAR9/12/2014 0:00	Friday	20149/19/2014 0:00	Friday	20149/26/2014 0:00	Friday	201410/3/2014 0:00	Friday	201410/10/2014 0:00	Friday	201410/17/2014 0:00	Friday	201410/24/2014 0:00	Friday	201410/31/2014 0:00	Friday	201411/7/2014 0:00	Friday	201412/12/2014 0:00	Friday	201412/19/2014 0:00	Friday	201412/26/2014 0:00	Friday	20141/2/2015 0:00	Friday	20151/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 Nwhere datepart(dw, dateadd(day, n, current_timestamp)) = 6order by FridayDate   | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     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 weekdeclare @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 Norder by FridayDate;   | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     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 datetimeDECLARE @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'------------------------------------------------------------------------------------------------------------------------;WITHcteTally10 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 dateFROM (    --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_FridayINNER JOIN cteTally1000 weeks ON    DATEADD(DAY, weeks.week# * 7, first_Friday) <= @ending_dateORDER BY date   | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                       
                          
                            
                                    | 
                                     nietzky 
                                    Yak Posting Veteran 
                                     
                                    
                                    75 Posts  | 
                                    
                                      
                                        
                                          
                                           
                                            Posted - 2015-02-04 : 12:08:35
                                          
  | 
                                         
                                        
                                          | Thank you all.  | 
                                         
                                        
                                            | 
                                         
                                       
                                     | 
                                   
                            
                            
                                | 
                                    
                                      
                                     
                                    
                                 | 
                             
                         
                     | 
                 
             
         |   
     
     
            
              
	     |  
		
			
     
          
		 |