LEFT function in SQL
The LEFT() function in SQL is used to extract a specified number of characters from the left side of a string.
SQL Syntax
LEFT(string, number_of_characters)
- string: The input string from which characters will be extracted.
- number_of_characters: The number of characters you want to extract from the left side of the string.
Example: Let's we have a table Employees.
Table: Employees
| ID | Name |
|---|---|
| 1 | Ashish |
| 2 | Priya |
| 3 | Rahul |
| 4 | Sneha |
Now we want to extract the first 3 characters from each employee's name .
SQL
SELECT LEFT(name, 3) AS ShortName FROM employees;
If the name is "Ashish", the result will be "Ash".
The result of the above query is shown below:
| ID | Name | ShortName |
|---|---|---|
| 1 | Ashish | Ash |
| 2 | Priya | Pri |
| 3 | Rahul | Rah |
| 4 | Sneha | Sne |