SUBSTRING_BEFORE_LAST

Description

Returns the substring that occurs before the last occurrence of the specified separator within the provided string. If the separator is not found, returns an empty string. The search is case-sensitive.

Syntax

SUBSTRING_BEFORE_LAST(string, separator) -> String

Parameters

ParameterData TypeRequiredDescription
stringStringYesThe original string.
separatorStringYesThe separator to search for within the string.

Example

SUBSTRING_BEFORE_LAST("REF-2025-1234", "-")
-- Returns "REF-2025".
-- Extracts the substring before the last occurrence of "-".

Tips

  • If the separator appears at the beginning of the string, the function returns an empty string.

  • The function returns the entire string if any of these occur:

    • the separator is not found in the string,

    • if the separator is an empty string.