CREATE FUNCTION dbo.GetBestAddressTypeNumber  (@ref int, @guest int)
RETURNS int
AS
BEGIN
--Created By Nenad Blagojevic 20040116
 declare @return int
 declare @returntemp int
 set @return = 0
  select top 1 @return=AddressType--, 
  from reservationaddress ra WITH (nolock) 
  inner join reservationaddressxtab rax WITH (nolock) on ra.addressnumber =  rax.addressnumber and referencenumber = @ref and guestnumber = @guest
  order by     Case 
      WHEN Addresstype = 2 then 1
      WHEN Addresstype = 3 then 2 
      WHEN Addresstype = 5 then 3 
      WHEN Addresstype = 1 then 4 
      WHEN Addresstype = 4 then 5 
      ELSE 6
    END
 return @return
end

