LEFT function
The LEFT function returns a substring consisting of the leftmost N characters from a string argument.
The function has this syntax:
Element | Description | Restrictions | Syntax |
---|---|---|---|
position | Ordinal position (from the left) in the string; this character and all to the left are to be returned | Must be an expression, constant, column, or host variable of a built-in integer type, or that can be converted to an integer | Expression |
source_string | Expression that evaluates to a character string | Must be an expression, constant, column, or host variable of a data type that can be converted to a character type | Expression |
The arguments to the LEFT function cannot be user-defined data types.
What the LEFT function returns depends on the number of logical characters in source_string and on the value of position:
- If source_string evaluates to a string with more than position characters, the return value is a substring of source_string, consisting of all characters to the left of the specified position.
- If source_string evaluates to a string with no more than position characters, the return value is the entire source_string.
- If source_string evaluates to NULL, or if position is zero or negative, then NULL is returned.
- If no position argument is specified, no string value is returned, and an exception is issued.
The return data type is the same as its source_string argument. If a host variable is the source, the return value is either NVARCHAR or NCHAR, according to the length of the returned string, using the return type promotion rules that the section Return Types from the CONCAT Function describes.
The following function expression requests the first five
characters of a quoted string:
LEFT('www.ibm.com',5)
In
this example, the LEFT function returns the substring www.i