Réponse acceptée !
Voila j'ai trouvé la solution :
DECLARE @Dt_Deb_Month AS datetime;
DECLARE @Dt_Fin_Month AS datetime;
SET @Dt_Deb_Month = convert(datetime, '2008-08-01');
SET @Dt_Fin_Month = DateAdd ("d", -1 ,DateAdd ("m", 1 ,@Dt_Deb_Month));
DECLARE @Nb_Jours_Mois AS integer;
SET @Nb_Jours_Mois = DateDiff("d", @Dt_Deb_Month , DateAdd ("m", 1 , @Dt_Deb_Month))
-- Donne le premier jour du mois, le dernier et le nombre de jour du mois
SELECT @Dt_Deb_Month, @Dt_Fin_Month, @Nb_Jours_Mois
SELECT
SUM( CASE WHEN Dt_Deb >= @Dt_Deb_Month AND Dt_FIn <= @Dt_Fin_Month THEN (DATEDIFF("d", Dt_Deb, Dt_Fin)+1)
WHEN Dt_Deb < @Dt_Deb_Month AND Dt_FIn <= @Dt_Fin_Month THEN (DATEDIFF("d", @Dt_Deb_Month, Dt_Fin)+1)
WHEN Dt_Deb >= @Dt_Deb_Month AND Dt_FIn > @Dt_Fin_Month THEN (DATEDIFF("d", Dt_Deb, @Dt_Fin_Month)+1)
WHEN Dt_Deb < @Dt_Deb_Month AND Dt_FIn > @Dt_Fin_Month THEN @Nb_Jours_Mois
END) as Nb_Jours, id_chambre
FROM reservation
WHERE ((Dt_Deb > @Dt_Deb_Month AND (Dt_Deb < @Dt_Fin_Month OR Dt_Fin < @Dt_Fin_Month)) OR
(@Dt_Deb_Month > Dt_Deb AND (@Dt_Deb_Month < Dt_Fin OR @Dt_Fin_Month < Dt_Fin)) OR
(Dt_Deb = @Dt_Deb_Month AND (Dt_Fin IS NOT NULL AND @Dt_Fin_Month IS NOT NULL)))
GROUP BY id_chambre
Citt_jr
Bats toi avec les meilleurs, crève avec le reste
[ Lien ]