Skip to main content

APPEND_TRAILING_CHAR_IF_ABSENT

Description

Used to add a specific character (such as a space, a specific symbol, etc.) to the end of a string if the character does not exist at the end of the string. The function is to ensure that the string ends with a specific character.

Syntax

APPEND_TRAILING_CHAR_IF_ABSENT ( <str> , <trailing_char> )

Parameters

ParametersDescription
<str>Target string to be judged
<trailing_char>Character to be added to the end of the string (if the character does not exist)

Return value

Parameters The string after concatenation of <str> and <trailing_char> (if <trailing_char> does not exist in <str>)

Example

  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ṛ |
+----------------------------------------------+