Skip to main content

APPEND_TRAILING_CHAR_IF_ABSENT

Description

The APPEND_TRAILING_CHAR_IF_ABSENT function ensures that a string ends with a specified character. If the character does not exist at the end of the string, it is added; if it already exists, the string remains unchanged.

Syntax

APPEND_TRAILING_CHAR_IF_ABSENT(<str>, <trailing_char>)

Parameters

ParameterDescription
<str>The target string to process. Type: VARCHAR
<trailing_char>The character that must appear at the end of the string. Type: VARCHAR

Return Value

Returns VARCHAR type:

  • If <trailing_char> does not exist at the end of <str>, returns the concatenation of <str> and <trailing_char>
  • If <trailing_char> already exists at the end of <str>, returns the original <str>

Special cases:

  • If any argument is NULL, returns NULL
  • If <str> is an empty string, returns <trailing_char>

Examples

  1. Basic usage: Add character when it doesn't exist
SELECT APPEND_TRAILING_CHAR_IF_ABSENT('a', 'c');
+------------------------------------------+
| append_trailing_char_if_absent('a', 'c') |
+------------------------------------------+
| ac |
+------------------------------------------+
  1. Character already exists, don't add
SELECT APPEND_TRAILING_CHAR_IF_ABSENT('ac', 'c');
+-------------------------------------------+
| append_trailing_char_if_absent('ac', 'c') |
+-------------------------------------------+
| ac |
+-------------------------------------------+
  1. Empty string handling
SELECT APPEND_TRAILING_CHAR_IF_ABSENT('', '/');
+------------------------------------------+
| append_trailing_char_if_absent('', '/') |
+------------------------------------------+
| / |
+------------------------------------------+
  1. NULL value handling
SELECT APPEND_TRAILING_CHAR_IF_ABSENT(NULL, 'c');
+-------------------------------------------+
| append_trailing_char_if_absent(NULL, 'c') |
+-------------------------------------------+
| NULL |
+-------------------------------------------+
  1. UTF-8 character
SELECT APPEND_TRAILING_CHAR_IF_ABSENT('acf', 'ṛ');
+----------------------------------------------+
| APPEND_TRAILING_CHAR_IF_ABSENT('acf', 'ṛ') |
+----------------------------------------------+
| acfṛ |
+----------------------------------------------+