SQL Left Function Example | LEFT() In SQL Server And MySQL
SQL LEFT() is used for extracting a substring starting from the left, followed by the number of characters. SQL LEFT() function extracts several characters from the string (starting from left).
SQL Left Function
SQL LEFT() function extracts a given number of characters from the left side of a supplied string.
The syntax of the LEFT() function is as follows.
SELECT LEFT (string, no_of_characters);
See the following parameters.
- String: An input string from which the substring will be extracted and can be a literal string, variable or a column.
- No_of_characters: Number of characters you wish to extract from the string.
The LEFT() function returns a value of VARCHAR when the input_string is non-Unicode character data type and returns an NVARCHAR if the input_string is a Unicode character data type.
Example of SQL LEFT()
SELECT LEFT ('AppDividend.com', 11);
Here 11 characters were displayed starting from the left-most part of the string.
SELECT LEFT ('AppDividend.com', 3);
Here 3 characters were displayed starting from the left-most part of the string.
SELECT LEFT ('SQL IS FUN', 4);
Here 4 characters were displayed starting from the left-most part of the string. After SQL there is whitespace which is also counted as a character.
SELECT LEFT ('SQL IS FUN', 20);
SQL IS FUN
Here the number of characters exceeded the length of the string as a whole result string is displayed as an output.
Let’s apply the LEFT function in a Table.
Now if we want to display the first 7 digits of the phone, then the following query has to be written:
Select First_name AS Emp_Name, LEFT (Phone, 7) AS First_seven_digit from Employee;
So, you can see that the first seven-digit of phone number is displayed under the column named Phone.
If the number_of_characters exceeds the length of the string, the LEFT function will return string.
MySQL LEFT() Function
MySQL LEFT() function extracts a number of characters from a string (starting from left).
SELECT LEFT("MySQL Tutorial", 3) AS ExtractString;
SQL Server LEFT() Function
The following statement uses LEFT() to return the three leftmost characters of the character string.
SELECT LEFT('Millie Bobby Brown', 6) Output_string;
Output_string ------------- Millie (1 row affected)
Finally, SQL Left Function Example | SQL Server And MySQL is over.