CREATE FUNCTION dbo.GetReferenceNumberFromTransactionNote (@ANote VARCHAR(50)) RETURNS INT AS
/*
  Created By Kent Johansen, December 2024
  Get the ReferenceNumber 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 @LReferenceNumber INT
  
  SET @LCharIdx = CHARINDEX('/', @ANote)
  IF @LCharIdx = 0
    SET @LReferenceNumber = @ANote
  ELSE
    SET @LReferenceNumber = SUBSTRING(@ANote, 0, @LCharIdx)
  
  RETURN @LReferenceNumber
END