declare @d1 datetime,@d2 datetime,@dx datetime,@x int,@mondays int
--the first date is taken from the sales table in the pubs
--database but u can use your own date.
--initialize first date
set @d1=(select max(ord_date) from sales)
--let's say the second date is 25 days away from first date
set @d2=dateadd(day,25,@d1)
--inititalize intermediate date variable
set @dx=@d1
-- initialize loop counter
set @x=0
set @mondays=0 --number of mondays
while (@dx<>@d2)
begin
set @dx=(select dateadd(day,@x,@d1))
if( (select datename(dw,@dx))='Monday')
set @mondays=@mondays+1
set @x=@x+1 --increment loop counter
end
select cast(@mondays as varchar(2))+ ' Mondays'
I hope this Helps.
Bertrandkis 'Black belt SQL Programmer'
/* I spent 3 hours to find out but worth it!
note: I initiated the thread
*/
declare @d1 datetime -- start date
declare @d2 datetime -- end date
declare @nb_days smallint
declare @wd smallint -- weekday to count
declare @j1 smallint -- weekday for start date
declare @j2 smallint -- weekday for endte
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.