- Categories:
String & binary functions (Matching/Comparison)
LEFT¶
Returns a leftmost substring of its input. LEFT(STR,N) is equivalent to SUBSTR(STR,1,N).
- See also:
Syntax¶
LEFT( <string_expr> , <length_expr> )
Arguments¶
string_expr
This must be a
VARCHAR
orBINARY
value.length_expr
The length should be an expression that evaluates to an integer. It should specify:
The number of UTF-8 characters to return if the input is
VARCHAR
.The number of bytes to return if the input is
BINARY
.
The length should be greater than or equal to zero. If the length is a negative number, the function returns an empty string.
Returns¶
The data type of the returned value is the same as the data type of the input value (BINARY
or VARCHAR
).
Collation details¶
Collation applies to VARCHAR
inputs. Collation does not apply if the input data type of the first parameter is
BINARY
.
Although collation is accepted syntactically, collations have no impact on processing. For example, languages with two-character and three-character letters (e.g. “dzs” in Hungarian, “ch” in Czech) still count those as two or three characters (not one character) for the length argument.
However, the returned value retains the collation specification of the first argument. This might be useful if the returned value is passed to another function as part of nested function calls.
Examples¶
SELECT LEFT('ABCDEF', 3);
-------------------+
LEFT('ABCDEF', 3) |
-------------------+
ABC |
-------------------+