Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-04-12
6
Medium Priority
?
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 26

Accepted Solution

by:
Hilaire earned 1000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

609 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