MS SQL Server RIGHT() Function

Sometime you may need to determine right part of specific string in MS SQL Server. There is a function called RIGHT in MS SQL Server by which you can do this. RIGHT function returns the left part of a character string with the specified number of characters.

Let’s see the function structure:

RIGHT(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 RIGHT function to determine right part of specific string.

DECLARE @input varchar(60);
SET @input = 'Minhajur Rahman Khan';
SELECT @input AS 'original_string', RIGHT(@input,4) AS 'right_4_string';

Here is the result set.

original_string right_4_string
Minhajur Rahman Khan Khan

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 right 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.

Add a Comment