Solved

how to get a list of friday dates between a date range

Posted on 2004-04-12
6
285 Views
Last Modified: 2006-11-17
I'd like to return all friday dates between a given range.

For instance, the range might be 1/1/2004 to 4/12/2004.

How can I return all friday's between those dates?
0
Comment
Question by:babybird
6 Comments
 
LVL 7

Expert Comment

by:ChrisFretwell
ID: 10809144
From an existing dataset or just a list of them?

if you want to return records from an existing table where a date field is between a range and on a friday, then this would work

where yourdate between '1/1/2004 ' and '4/12/2004'
and datepart(dw,yourdate) = 6  ----provided you kept the default week start of Sunday

if you want to generate a list, then this will work (this could be made easier, but this is fast and accurate )


declare @numweeks int
declare @startdate datetime
declare @enddate datetime
declare @printstring varchar(12)
declare @currdate datetime

set @startdate = '1/1/2004'
set @enddate = '4/12/2004'


select @numweeks = datediff(wk,@startdate,@enddate)

set @currdate = @startdate

while @currdate < @enddate begin
      if datepart(dw,@currdate) = 6 begin
            set @printstring = convert(char(12),@currdate,101)
            print @printstring
      end
      set @currdate = dateadd(d,1,@currdate)
end

0
 
LVL 3

Expert Comment

by:edwardsearch
ID: 10810799
You can use table variable to return as result set

declare @ltbl table (tmpdate datetime)
declare @startdate datetime
declare @enddate datetime
declare @printstring varchar(12)
declare @currdate datetime

set @startdate = '1/1/2004'
set @enddate = '4/12/2004'

set @currdate = @startdate

while @currdate < @enddate
begin
 if datepart(dw,@currdate) = 6
    insert into @ltbl (tmpdate) values (@currdate)
  set @currdate = dateadd(d,1,@currdate)
end
select tmpdate from @ltbl

- Edward
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 10811190
Hi babybird,

declare @test datetime

set @test = @startdate

select @test as Fri into @friday where 0=1

While @test <= @Enddate
begin
    if datename(dw,@test) = 'FRIDAY'
    begin
        insert into @Friday Values(@test)
    end
    Set @test=Dateadd(d,1,@test)
end

Select * from @friday
 order by 1

Cheers!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 26

Accepted Solution

by:
Hilaire earned 250 total points
ID: 10811228
Create Function dbo.GetFridays(@StartDate DateTime, @EndDate datetime)
declare @fridays table (f_date datetime)
AS
Begin
      set @StartDate = DateAdd(d, (20-@@datefirst-datepart(dw, @StartDate))%7, @StartDate)
      while @StartDate <= @EndDate
      begin
            insert into @fridays(f_date) values(@StartDate)
            set @StartDate = dateadd(d,7,@StartDate)
      end
      return
End
Go

select * from dbo.getfridays('2004.01.01', '2004.04.12' )

0
 
LVL 26

Expert Comment

by:Hilaire
ID: 10811312
The code searches for the next friday and then steps week per week instead of day per day

DateAdd(d, (20-@@datefirst-datepart(dw, @StartDate))%7, @StartDate) finds next friday
It is not affected by date settings, ie it will still work even if you issue a
SET DATEFIRST    statement with non-default values

HTH

Hilaire
0
 

Author Comment

by:babybird
ID: 10824131
Thanks to all who helped!!  I learned something from all your methods.  That's why I love this forum so much....I always learn new and valuable things.  

I decided to go with the function call...just fyi for anyone that might utilize this code....to get it to compile and run, I changed the declare statement to a returns statement and it worked fine.

Thanks again!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now