LEFT¶
The LEFT function in SQL returns the specified number of characters from the start (left side) of a given string.
Syntax¶
Parameters¶
string: The original string from which to extract characters.number: The number of characters to extract from the start of the string. This must be an integer.
Return Value¶
The function returns a string, which consists of the specified number of characters from the start of the original string. If the original string is shorter than the specified number, the function returns the whole string.
Errors¶
- If the
numberargument is not an integer, aFunctionRequiresIntegerValueerror will be returned. - If the
stringargument is not a string, aFunctionRequiresStringValueerror will be returned.
Examples¶
Consider a table Item created and filled with the following data:
CREATE TABLE Item (
name TEXT DEFAULT LEFT('abc', 1)
);
INSERT INTO Item VALUES ('Blop mc blee'), ('B'), ('Steven the &long named$ folken!');
You can use the LEFT function to extract the first three characters of each name:
This will return:
Note that when the string length is less than the specified number (as with 'B'), the function will return the whole string.