Skip to main content

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​

ParameterDescription
<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​

  1. Basic usage
SELECT strleft('Hello doris', 5);
+---------------------------+
| strleft('Hello doris', 5) |
+---------------------------+
| Hello |
+---------------------------+
  1. Handling negative length
SELECT strleft('Hello doris', -5);
+----------------------------+
| strleft('Hello doris', -5) |
+----------------------------+
| |
+----------------------------+
  1. Handling NULL parameter
SELECT strleft('Hello doris', NULL);
+------------------------------+
| strleft('Hello doris', NULL) |
+------------------------------+
| NULL |
+------------------------------+
  1. Handling NULL string
SELECT strleft(NULL, 3);
+------------------+
| strleft(NULL, 3) |
+------------------+
| NULL |
+------------------+