RIGHT

RIGHT in SQL is a string function that extracts a certain number of characters from the right end of a specified string.

RIGHT(str, len)

  • str: The string from which the rightmost characters will be extracted.
  • len: The number of characters to extract from the end (right side) of the string.

Example

SELECT RIGHT('SQL Documentation', 5) AS 'Right Output';

Output

+--------------+
| Right Output |
+--------------+
| ation |
+--------------+

Explanation

In this example, the RIGHT() function is used to extract the specified number of characters from the end of the provided string. The string ‘SQL Documentation’ is provided, and the function is asked to return the last 5 characters - ‘ation’.

RIGHT(string text, n integer)

  • string text: This is the source string from which characters are to be extracted. It can be any text, character varying, or character type.
  • n integer: This is the number of characters to be extracted from the end of the source string text. It should be of type integer and if n is less than 1, the result is an empty string. If n exceeds the length of string text, all of the string text is returned.

Example

SELECT RIGHT('PostgreSQL', 3);

Output

"SQL"

Explanation

The RIGHT function in PostgreSQL returns the last n characters from a string. In the provided example, it returned the last three characters from the string ‘PostgreSQL’, which are ‘SQL’.

RIGHT( character_expression , integer_expression )

  • character_expression: The string of characters from which the rightmost characters are returned. Can be a literal string, variable, or column of either character or binary data.
  • integer_expression: A positive integer that specifies how many rightmost characters of the character_expression will be returned. If negative, an error is returned. If zero, an empty string is returned.

Example

SELECT RIGHT('SQL Server', 6) AS Result;

Output

Result
------
Server

Explanation

The RIGHT function in SQL Server extracts a specified number of characters from a string, starting from the rightmost character. In this example, it returns the last 6 characters from the string ‘SQL Server’, which is ‘Server’.

RIGHT(text, number)

  • text: The string from which the rightmost characters will be extracted.
  • number: Specifies the number of characters to extract from the right side of the text string.

Example

SELECT RIGHT('Hello World', 5) FROM dual;

Output

World

Explanation

The RIGHT function is used to extract a specific number of characters from the right side of a string. In this example, 5 characters are extracted from the right side of the string ‘Hello World’. The output is ‘World’.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.