MS SQL Server LEFT() Function
Sometime you may need to determine left part of specific string in MS SQL Server. There is a function called LEFT
in MS SQL Server by which you can do this. LEFT
function returns the left part of a character string with the specified number of characters..
Let’s see the function syntax:
LEFT(character_expression,integer_expression);
In this syntax:
- In this function there are two parameters.
- Parameter character_expression is an expression of character or binary data.
- Parameter character_expression can be a constant, variable, or column.
- Parameter integer_expression Is a positive integer that specifies how many characters of the character_expression will be returned. If integer_expression is negative, an error is returned. If integer_expression is type bigint and contains a large value, character_expression must be of a large data type such as varchar(max). The integer_expression parameter counts a UTF-16 surrogate character as one character.
- Returns varchar when character_expression is a non-Unicode character data type. Returns nvarchar when character_expression is a Unicode character data type. Returns the left part of a character string with the specified number of characters.
The following example uses LEFT
function to determine right part of specific string.
DECLARE @input varchar(60); SET @input = 'Minhajur Rahman Khan'; SELECT @input AS 'original_string', LEFT(@input,6) AS 'left_6_string';
Here is the result set.
original_string | left_6_string |
---|---|
Minhajur Rahman Khan | Minhaj |
I have an another post MS SQL Server String Related Functions where you will learn about different string related functions of MS SQL Server.
In this tutorial, I have shown you how to determine left part of a character string with the specified number of characters in MS SQL Server. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page http://www.facebook.com/freetechtrainer and stay connected.