Python

SQL ends_with

When working with string types in SQL, you will come across an instance where you need to check whether a string ends with a specific substring. This allows you to check if a specific substring is a suffix of another string.

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:

ENDS_WITH(value1, value2)

 
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.

WITH
  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:

results
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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list