CREATE FUNCTION dbo.GetGuestNumberFromTransactionNote (@ANote VARCHAR(50)) RETURNS INT AS
/*
  Created By Kent Johansen, December 2024
  Get the GuestNumber part of transaction.note on owner transactions
  Input will be in format ReferenceNumber/Guestnumber like 20654/1, for ref.: 20654 guest: 1
*/
BEGIN
  DECLARE @LCharIdx INT
  DECLARE @LGuestNumber INT
  
  SET @LCharIdx = CHARINDEX('/', @ANote)
  IF @LCharIdx = 0
    SET @LGuestNumber = 0
  ELSE
    SET @LGuestNumber = SUBSTRING(@ANote, @LCharIdx+1, 20)
  
  RETURN @LGuestNumber
END