strleft() function

The strleft() function returns the leftmost n characters from an input string.

Syntax

The strleft() function has the following syntax:
varchar = strleft(varchar input, int n);
nvarchar = strleft(nvarchar input, int n);

The input value specifies the varchar or nvarchar value from which the characters are returned.

The n value specifies the number of characters to return. If the input value is shorter than the number of characters that are requested, the function returns the entire input string.

Returns

The function returns the leftmost n characters from the input string.

Example

select strleft ('1234567891',5);
 STRLEFT
---------
 12345
(1 row)

select strleft ('12',5);
 STRLEFT
---------
 12
(1 row)