Code :
CREATE procedure nbJourSemaine @dateDebut date, @dateFin date, @jourSemaine int AS BEGIN declare @nbSemaines int declare @joursRestants int SET @nbSemaines = datediff(day, @dateDebut, @dateFin)/7 SET @joursRestants = datediff(day, @dateDebut, @dateFin)%7 SELECT @nbSemaines+ ( CASE when @joursRestants >0 then CASE when datepart(weekday, dateadd(day, -1*@joursRestants, @dateFin)) > DATEPART(weekday,@dateFin) then case when @jourSemaine < datepart(weekday, dateadd(day, -1*@joursRestants, @dateFin)) AND @jourSemaine > DATEPART(weekday,@dateFin) then 0 else 1 end when datepart(weekday, dateadd(day, -1*@joursRestants, @dateFin)) < DATEPART(weekday,@dateFin) then case when @jourSemaine >= datepart(weekday, dateadd(day, -1*@joursRestants, @dateFin)) AND @jourSemaine <= DATEPART(weekday,@dateFin) then 1 else 0 end end else 0 end ) END;
|