What is RIGHT() in SQL?

The RIGHT() function returns a specific number of characters from the right side of a string.

Figure 1 shows a visual representation of the RIGHT() function.

Figure 1: Visual representation of RIGHT() function

Syntax

RIGHT(string, numOfChars)

Parameters

The RIGHT() function requires two parameters:

  • A string from which the characters need to be extracted.

If a number instead of a string is sent as a parameter, then the number will be cast to a string and then the function will be applied.

  • Number of characters that need to be extracted.

Return value

The RIGHT() function returns the specific number of characters from the right side of a string sent as a parameter.

If the number of characters is greater than the actual length of the string, then the function returns the entire string.

Code

-- string sent
SELECT RIGHT('HELLO',3);
-- number of characters greater than length of string
SELECT RIGHT('HELLO',7);
-- number is sent instead of string
-- it will be cast as a string and then right() is applied
SELECT RIGHT(12345,2);