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.

.
Note: The LEFT() function extracts a number of characters from a string (i.e. starting from left).

SQL Left Function Syntax

The syntax of the SQL Left Function is

LEFT ( input_string , number_of_characters )

In this syntax:

  • input_string: Required. The input_string can be a literal string, column or variable. The data type of the result of the input_string can be any data type, and except for TEXT or NTEXT, that is certainly converted to VARCHAR or NVARCHAR.
  • number_of_characters: Required. the number_of_characters is a positive integer which specifies the number of characters of the input_string will be returned.
.
Note: This function in SQL Server returns a value of 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:


left function in sql
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

SQL LEFT Example

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:


SQL Server LEFT function Example

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!