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
| Parameter | Description |
|---|---|
<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
- 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 |
+------------------------------------------+
- Character already exists, don't add
SELECT APPEND_TRAILING_CHAR_IF_ABSENT('ac', 'c');
+-------------------------------------------+
| append_trailing_char_if_absent('ac', 'c') |
+-------------------------------------------+
| ac |
+-------------------------------------------+
- Empty string handling
SELECT APPEND_TRAILING_CHAR_IF_ABSENT('', '/');
+------------------------------------------+
| append_trailing_char_if_absent('', '/') |
+------------------------------------------+
| / |
+------------------------------------------+
- NULL value handling
SELECT APPEND_TRAILING_CHAR_IF_ABSENT(NULL, 'c');
+-------------------------------------------+
| append_trailing_char_if_absent(NULL, 'c') |
+-------------------------------------------+
| NULL |
+-------------------------------------------+
- UTF-8 character
SELECT APPEND_TRAILING_CHAR_IF_ABSENT('acf', 'ṛ');
+----------------------------------------------+
| APPEND_TRAILING_CHAR_IF_ABSENT('acf', 'ṛ') |
+----------------------------------------------+
| acfṛ |
+----------------------------------------------+