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 usage
SELECT strleft('Hello doris', 5);
+---------------------------+
| strleft('Hello doris', 5) |
+---------------------------+
| Hello |
+---------------------------+
- Handling negative length
SELECT strleft('Hello doris', -5);
+----------------------------+
| strleft('Hello doris', -5) |
+----------------------------+
| |
+----------------------------+
- Handling NULL parameter
SELECT strleft('Hello doris', NULL);
+------------------------------+
| strleft('Hello doris', NULL) |
+------------------------------+
| NULL |
+------------------------------+
- Handling NULL string
SELECT strleft(NULL, 3);
+------------------+
| strleft(NULL, 3) |
+------------------+
| NULL |
+------------------+