STRLEFT
Description
The STRLEFT function returns a specified number of characters from the left side of a string. The length is measured in UTF8 characters.
Alias
LEFT
Syntax
STRLEFT(<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
- Returns empty string "" if len is less than or equal to 0
- Returns the entire string if len is greater than the string length
Examples
- Basic left extraction
SELECT STRLEFT('Hello doris', 5), LEFT('Hello doris', 5);
+---------------------------+----------------------+
| STRLEFT('Hello doris', 5) | LEFT('Hello doris', 5) |
+---------------------------+----------------------+
| Hello | Hello |
+---------------------------+----------------------+
- Different extraction lengths
SELECT STRLEFT('Hello World', 3), STRLEFT('Hello World', 8);
+----------------------------+----------------------------+
| STRLEFT('Hello World', 3) | STRLEFT('Hello World', 8) |
+----------------------------+----------------------------+
| Hel | Hello Wo |
+----------------------------+----------------------------+
- NULL value handling
SELECT STRLEFT(NULL, 5), STRLEFT('Hello doris', NULL);
+------------------+------------------------------+
| STRLEFT(NULL, 5) | STRLEFT('Hello doris', NULL) |
+------------------+------------------------------+
| NULL | NULL |
+------------------+------------------------------+
- Empty string and zero length
SELECT STRLEFT('', 5), STRLEFT('Hello World', 0);
+------------------+----------------------------+
| STRLEFT('', 5) | STRLEFT('Hello World', 0) |
+------------------+----------------------------+
| | |
+------------------+----------------------------+
- Negative length handling
SELECT STRLEFT('Hello doris', -5), STRLEFT('Hello doris', -1);
+-----------------------------+----------------------------+
| STRLEFT('Hello doris', -5) | STRLEFT('Hello doris', -1) |
+-----------------------------+----------------------------+
| | |
+-----------------------------+----------------------------+
- Length exceeds string length
SELECT STRLEFT('ABC', 10), STRLEFT('short', 20);
+--------------------+----------------------+
| STRLEFT('ABC', 10) | STRLEFT('short', 20) |
+--------------------+----------------------+
| ABC | short |
+--------------------+----------------------+
- UTF-8 multi-byte characters
SELECT STRLEFT('ṭṛì ḍḍumai hello', 3), STRLEFT('ṭṛì ḍḍumai hello', 7);
+---------------------------------+----------------------------------+
| STRLEFT('ṭṛì ḍḍumai hello', 3) | STRLEFT('ṭṛì ḍḍumai hello', 7) |
+---------------------------------+----------------------------------+
| ṭṛì | ṭṛì ḍḍu |
+---------------------------------+----------------------------------+
- Number and ID prefix
SELECT STRLEFT('ID123456789', 5), STRLEFT('USER_987654321', 5);
+----------------------------+------------------------------+
| STRLEFT('ID123456789', 5) | STRLEFT('USER_987654321', 5) |
+----------------------------+------------------------------+
| ID123 | USER_ |
+----------------------------+------------------------------+
Keywords
STRLEFT, LEFT