| by Arround The Web | No comments

SQL Server Right Function

In this post, we will learn how to use the right function in the SQL Server. The right() function allows us to extract a specific number of characters to the right of a given input string.

Function Syntax, Parameters, and Return Value

The follwoing is the syntax of the right function in the SQL Server:

RIGHT ( character_expression , integer_expression )

The function arguments include:

  1. character_expression – This argument defines the input string from which the characters are extracted. This value can be of a literal string, a variable, or a table column. You can provide the value of this argument as of any type except TEXT or NTEXT. If so, SQL server converts them to VARCHAR and NVARCHAR, respectively.
  2. integer_expression – This argument refers to a positive integer value that determines the number of characters to be extracted from the input string.

The function returns VARCHAR type for non-Unicode input string and NVARCHAR for Unicode input string.


The following section provides some basic examples of using the right() function in the SQL Server.

Example 1: Basic Usage

Consider the following example provided:

SELECT RIGHT('https://geekbits.io', 11) AS url;

Once we run the given query, it should return the 11 characters from the right of the input string as shown in the following:

url        |

Example 2: Using the Right() Function with the Table Column

The following example illustrates how we can use the right() function to extract the last values in a given column.

Suppose we have a table as shown in the following:

We can extract the year from the product_name column as shown in the following query:

SELECT product_name, RIGHT(product_name, 4) AS YEAR
FROM products;

The resulting table is as follows:


In this post, you learned how to use the right function in the SQL Server to extract a set of characters from the right of a given string.

Thanks for reading!

Share Button

Source: linuxhint.com

Leave a Reply