CREATE FUNCTION dbo.IsOldReservation(@ReferenceNumber INT, @ADateEnd DATETIME)
RETURNS BIT AS  
/*
  NB20060118 Created
*/
BEGIN 
  DECLARE 
    @ResCount INT,
    @IsOld BIT
  
  SELECT @ResCount = COUNT(*)  FROM Reservations WITH (NOLOCK) 
  WHERE ReferenceNumber = @ReferenceNumber AND (CONVERT(DATETIME,ArrivalDate,112)>=@ADateEnd OR CONVERT(DATETIME,DepartureDate,112)>=@ADateEnd)
  IF ISNULL(@ResCount,0) = 0 AND (SELECT dbo.IsGroupNOX(@ReferenceNumber))=1
    SET @IsOld = 1
  ELSE
    SET @IsOld = 0
  RETURN  @IsOld
END

