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

RIGHT , SUBSTR , SUBSTRING

Syntax

LEFT( <string_expr> , <length_expr> )

Arguments

string_expr

This must be a VARCHAR or BINARY 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.

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('ABCDEFG', 3);

-------------------+
 LEFT('ABCDEF', 3) |
-------------------+
 ABC               |
-------------------+