STRRIGHT
Description
The STRRIGHT function returns a specified number of characters from the right side of a string. The length is measured in UTF8 characters.
Alias
RIGHT
Syntax
STRRIGHT(<str>, <len>)
Parameters
| Parameter | Description |
|---|---|
<str> | The string to extract from. Type: VARCHAR |
<len> | The number of characters to return. Type: INT |
Return Value
Returns VARCHAR type, representing the extracted substring.
Special cases:
- Returns NULL if any argument is NULL
- If len is negative, returns the substring starting from the abs(len)th character from the right
- Returns the entire string if len is greater than the string length
Examples
- Basic right extraction
SELECT STRRIGHT('Hello doris', 5), RIGHT('Hello doris', 5);
+----------------------------+------------------------+
| STRRIGHT('Hello doris', 5) | RIGHT('Hello doris', 5) |
+----------------------------+------------------------+
| doris | doris |
+----------------------------+------------------------+
- Different extraction lengths
SELECT STRRIGHT('Hello World', 3), STRRIGHT('Hello World', 8);
+-----------------------------+-----------------------------+
| STRRIGHT('Hello World', 3) | STRRIGHT('Hello World', 8) |
+-----------------------------+-----------------------------+
| rld | lo World |
+-----------------------------+-----------------------------+
- NULL value handling
SELECT STRRIGHT(NULL, 5), STRRIGHT('Hello doris', NULL);
+-------------------+-------------------------------+
| STRRIGHT(NULL, 5) | STRRIGHT('Hello doris', NULL) |
+-------------------+-------------------------------+
| NULL | NULL |
+-------------------+-------------------------------+
- Empty string and zero length
SELECT STRRIGHT('', 5), STRRIGHT('Hello World', 0);
+-------------------+-----------------------------+
| STRRIGHT('', 5) | STRRIGHT('Hello World', 0) |
+-------------------+-----------------------------+
| | |
+-------------------+-----------------------------+
- Negative length handling
SELECT STRRIGHT('Hello doris', -7), STRRIGHT('Hello doris', -5);
+-----------------------------+-----------------------------+
| STRRIGHT('Hello doris', -7) | STRRIGHT('Hello doris', -5) |
+-----------------------------+-----------------------------+
| doris | o doris |
+-----------------------------+-----------------------------+
- Length exceeds string length
SELECT STRRIGHT('ABC', 10), STRRIGHT('short', 20);
+---------------------+-----------------------+
| STRRIGHT('ABC', 10) | STRRIGHT('short', 20) |
+---------------------+-----------------------+
| ABC | short |
+---------------------+-----------------------+
- UTF-8 multi-byte characters
SELECT STRRIGHT('ṭṛì ḍḍumai hello', 5), STRRIGHT('ṭṛì ḍḍumai hello', 11);
+----------------------------------+-----------------------------------+
| STRRIGHT('ṭṛì ḍḍumai hello', 5) | STRRIGHT('ṭṛì ḍḍumai hello', 11) |
+----------------------------------+-----------------------------------+
| hello | ḍumai hello |
+----------------------------------+-----------------------------------+
- Numeric string handling
SELECT STRRIGHT('123456789', 3), STRRIGHT('ID_987654321', 6);
+----------------------------+-------------------------------+
| STRRIGHT('123456789', 3) | STRRIGHT('ID_987654321', 6) |
+----------------------------+-------------------------------+
| 789 | 654321 |
+----------------------------+-------------------------------+
- Email domain extraction
SELECT STRRIGHT('user@example.com', 11), STRRIGHT('admin@company.org.cn', 14);
+----------------------------------+--------------------------------------+
| STRRIGHT('user@example.com', 11) | STRRIGHT('admin@company.org.cn', 14) |
+----------------------------------+--------------------------------------+
| example.com | company.org.cn |
+----------------------------------+--------------------------------------+
Keywords
STRRIGHT, RIGHT