In this article, you will learn how to use Standard SQL functions to determine if a string ends with a specific value.
Function Syntax
In Standard SQL, we use the ends_with() function to determine if a substring is a suffix of another.
The syntax is expressed below:
The function will take the value1 and value2 as string or a sequence of bytes. It will then evaluate if value2 is a suffix of value1.
If true, the function will return a Boolean TRUE; otherwise, the function will return FALSE.
Example
The code below shows how we can use the ends_with function to check if a string ends with a given value.
vars AS (
SELECT
'MySQL' AS var
UNION ALL
SELECT
'PostgreSQL' AS var
UNION ALL
SELECT
'Standard SQL' AS var
UNION ALL
SELECT
'SQL Server' AS var)
SELECT
ENDS_WITH(var, 'SQL') AS results
FROM
vars;
The function will evaluate each of the string and determine if it ends with ‘SQL’. The function will then return the results as Boolean values as shown in the output below:
true
true
true
false
From the output above, we can see that the first three strings end with ‘SQL’.
Keep in mind that the same operation is supported on byte strings.
Termination
In this article, we explored how we can use the ends_with function in Standard SQL to check if a string ends with a given value.