SQL Server RIGHT() Function: Returns Chars from Right Side

In SQL Server, the RIGHT() function returns the specified number of characters from the right side of the specified string.

RIGHT(string_expression, number_of_charaters)

Parameters

string_expression: string or binary data. It can be a constant, a variable, or a table column. It can be any data type except text or ntext, and can be implicitly converted to varchar or nvarchar.

number_of_charaters: A positive integer that specifies how many characters of the string_expression should return. If number_of_charaters is negative, then an error is returned.

Return Value

Returns varchar when string_expression is a non-Unicode character data type.

Returns nvarchar when string_expression is a Unicode character data type.

Example 1:

In this following example, the RIGHT() function returns the rightmost three characters of the specified string.

Example: RIGHT()
SQL Query: SELECT RIGHT('Have a nice day', 3) AS Result

Example 2:

In the following example, the rightmost four characters of the FirstName column are returned.

Example: RIGHT()
SELECT FirstName, RIGHT(FirstName,4) AS Result FROM Employee;
Want to check how much you know SQL Server?