SQL Left | Understanding The SQL LEFT Function With Example
SQL Left Function Summary: In this tutorial, you will learn about Format SQL LEFT function
and how to use them to extract a number of characters from the left side of a given string.
Definition and Usage
The SQL Server left function is a String Function that returns the leftmost characters from in the given expression. Thisfunction uses its 2nd argument to decide that how many left characters it should return.
SQL Left Function Syntax
The syntax of the SQL Left Function is
In this syntax:
- input_string: Required. The
input_string
can be a literal string, column or variable. The data type of the result of theinput_string
can be any data type, and except forTEXT
orNTEXT
, that is certainly converted toVARCHAR
orNVARCHAR
. - number_of_characters: Required. the
number_of_characters
is a positive integer which specifies the number of characters of theinput_string
will be returned.
VARCHAR
once the input_string
could be a non-Unicode character data type or NVARCHAR
if the input_string
could be a Unicode character data type.SQL LEFT() Function Examples
Let’s see the example of using the SQL LEFT function to understand it in better way.
A) Using SQL LEFT() function with a verbal character string
The given below following statement uses the LEFT() function to return the eight(8) leftmost characters of the character string SQL Server:
1 2 3 4 5 |
SELECT LEFT('Tutorial SQL Server',8) Result_string; SELECT LEFT('Tutorial SQL Server',5) Result_string; SELECT LEFT('Tutorial SQL Server',10) Result_string; SELECT LEFT('Tutorial SQL Server',15) Result_string; SELECT LEFT('Tutorial SQL Server',51) Result_string; |
Therefore, as you can see in the above result, how to use the LEFT function, and get the different result as you want.
B) Using the LEFT() function with a table column
The following example returns the five leftmost characters of each person name in the Persons table:
PersonID | Name | Address | City |
---|---|---|---|
1 | Noah Elijah | Bridgetown | Barbados |
2 | William Josiah | Brussels | Belgium |
3 | Elijah Connor | Zagreb | Croatia |
4 | Benjamin Hunter | San Jose | Costa Rica |
5 | Alexander Landon | Roseau | Dominica |
6 | Jacob Jonathan | Paris | France |
7 | Daniel Jeremiah | Libreville | Gabon |
8 | Owen Dominic | Athens | Greece |
9 | Gabriel Roman | Tegucigalpa | Honduras |
10 | Mateo Ezekiel | Budapest | Hungary |
1 2 3 4 5 6 7 |
SELECT Name, LEFT(Name, 5) first_5_characters FROM Persons ORDER BY Name; |

Using LEFT() function with GROUP BY clause
The given below following example uses the LEFT() function to return a set of initials of the Persons name and the number of each name for each initial:
1 2 3 4 5 6 7 8 9 |
SELECT LEFT(Name, 1) initial, COUNT(Name) name_count FROM Persons GROUP BY left(Name, 1) ORDER BY initial; |
Conclusion:
you have learned about this function and how to use them to extract a number of characters from the left side of a given string. If have any query, feel free to contact me, I will try to resolve your problem. I hope you will this enjoy article!