Tag: LEFT() and RIGHT() functions in SQL Server

LEFT() and RIGHT() functions in SQL Server

Extracts defined number of characters from the string provided, from left or right depending on LEFT() or RIGHT() function used.

That means Left(‘string’,3) returns “str”, 3 characters from the left side. Likewise, RIGHT does it from right side of the string.

A simple representation as below:

SELECT 'Latheesh' Fullname,Left('Latheesh', 4) AS LeftString,RIGHT('Latheesh', 4) AS RightString;

Another common use case is when we would like to add the ‘0’ characters prefixing depending on available numbers.For an example, if we need to represent digit like as always 6 characters irrespective the actual number by adding “0” (s) to the digit, we can easily achieve with RIGHT() function.
Declare @mydigit int 

Set @mydigit = '34'
Select Right('000000'+cast(@mydigit as varchar(6)),6)

Set @mydigit = '100'
Select Right('000000'+cast(@mydigit as varchar(6)),6)

I’d like to grow my readership. If you enjoyed this blog post, please share it with your friends!