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 |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-06-27 : 09:07:06
|
| pradeep m.m. writes "hi,i have 2 date values like '1/25/2002' and '5/25/2002'and i need to find out how many fridays occur between these datesas these dates are not stored in any table this is a user inputanswer as quickly as possible byepradeep" |
|
|
macka
Posting Yak Master
162 Posts |
Posted - 2002-06-27 : 11:45:55
|
| Only Fridays between these dates ??What if date1 and date2 both fall on Fridays ? Should they both be included - or just one - or none ?macka. |
 |
|
|
Kevin Snow
Posting Yak Master
149 Posts |
Posted - 2002-06-27 : 12:07:32
|
quote: Only Fridays between these dates ??What if date1 and date2 both fall on Fridays ? Should they both be included - or just one - or none ?macka.
Here's my stab at it.Create proc Friday(@date1 datetime,@date2 datetime, @Fridays int output)ASDECLARE @Firstday intSet @Firstday=datepart(dw,@date1)--counts 1 friday for every FULL weekset @fridays=cast(@date2-@date1 as integer)/7--adds an additional friday after comparing the start day to the --number of days in any 'fractional' weeks remainingset @fridays=@fridays+Case when (cast((@date2-@date1) as integer)%7)+@Firstday>5 then 1 else 0 end--If you DON'T want to include the last Friday, change )+(@Firstday%7)>5 ---to @Firstday>6Call it as-Declare @fred intexec Friday '7/5/2002','7/12/2002',@fred outputSelect @FredEdited by - kevin snow on 06/27/2002 12:09:52Edited by - kevin snow on 06/27/2002 14:58:10 |
 |
|
|
Kevin Snow
Posting Yak Master
149 Posts |
Posted - 2002-06-27 : 15:03:57
|
| OOPS!Sorry. Screwed it up for first day being a Saturday.modified "+ @firstday" to "+ (@Firstday%7)">This includes the Friday of the start date, and the end date if it is also Friday. |
 |
|
|
mpradeep
Starting Member
1 Post |
Posted - 2002-06-27 : 22:56:57
|
| @stdate datetime,@eddate datetime,declare @fwkday varchar(10)declare @day varchar(10)declare @cnt intselect @cnt=0select @fwkday='Friday'WHILE (@stdate<=@eddate)BEGIN select @day=(select datename(dw,@stdate))if (@day=@fwkday) begin select @cnt=@cnt +1 end select @stdate=(SELECT DATEADD(day,1,@stdate)) IF (@stdate > @eddate) BREAK ELSE CONTINUEENDprint @cntGO |
 |
|
|
|
|
|
|
|