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!

One thought on “LEFT() and RIGHT() functions in SQL Server”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s