SQL Server Right Function
Function Syntax, Parameters, and Return Value
The follwoing is the syntax of the right function in the SQL Server:
The function arguments include:
- 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.
- 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.
Examples:
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:
Once we run the given query, it should return the 11 characters from the right of the input string as shown in the following:
-----------+
geekbits.io|
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:
FROM products;
The resulting table is as follows:
Conclusion
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!
Source: linuxhint.com