LEFT()

Function Description

The LEFT() function returns the leftmost length character in the str string. If the str or len parameter is NULL, the NULL value is returned.

Function Syntax

Text
> LEFT(str,len)

Parameter definition

Parameters

Description

str

Required parameters. The string to extract the substring.

len

Required parameters. is a positive integer that specifies the number of characters to be returned from the left.
If len is 0 or negative, the LEFT function returns an empty string.
If len is greater than the length of the str string, the LEFT function returns the entire str string.

Example

Text
mysql> select left('abcde', 3) from dual;
+----------------+
| left(abcde, 3) |
+----------------+
| abc            |
+----------------+
1 row in set (0.00 sec)

drop table if exists t1;
CREATE TABLE t1 (str VARCHAR(100) NOT NULL, len INT);
insert into t1 values('abcdefghijklmn',3);
insert into t1 values(' ABCDEFGH123456', 3);
insert into t1 values('ABCDEF GHIJKLMN', 20);
insert into t1 values('ABCDEFGHijklmn ', -1);
insert into t1 values('ABCDEFGH123456', -35627164);
insert into t1 values('', 3);
mysql> select left(str, len) from t1;
+-----------------+
| left(str, len)  |
+-----------------+
| abc             |
| A               |
| ABCDEF GHIJKLMN |
|                 |
|                 |
|                 |
+-----------------+
6 rows in set (0.01 sec)

mysql> select left('sdfsdfsdfsdf', len) from t1;
+-------------------------+
| left(sdfsdfsdfsdf, len) |
+-------------------------+
| sdf                     |
| sdf                     |
| sdfsdfsdfsdfsdf         |
|                         |
|                         |
| sdf                     |
+-------------------------+
6 rows in set (0.01 sec)
Last updated on