RIGHT function
The RIGHT function returns a substring consisting of the rightmost N characters from a string argument.
The function has this syntax:
Element | Description | Restrictions | Syntax |
---|---|---|---|
position | Ordinal position (from the right) in the string; this character and all to the right are to be returned | Must be an expression, constant, column, or host variable of a built-in integer type, or that can be converted to an integer | Expression |
source_string | Expression that evaluates to a character string | Must be an expression, constant, column, or host variable of a data type that can be converted to a character type | Expression |
The arguments to the RIGHT function cannot be user-defined data types.
In left-to-right locales, such as the default U.S. English locale, this function returns a substring of trailing characters from the source_string.
What the RIGHT function returns depends on the number of logical characters in source_string and on the value of position:
- If source_string evaluates to a string with more than position characters, the return value is a substring of source_string, consisting of all characters to the right of the specified position.
- If source_string evaluates to a string with no more than position characters, the return value is the entire source_string.
- If source_string evaluates to NULL, or if position is zero or negative, then NULL is returned.
- If no position argument is specified, no string value is returned, and an exception is issued.
The return data type is the same as its source_string argument. If a host variable is the source, the return value is either NVARCHAR or NCHAR, according to the length of the returned string, using the return type promotion rules that the section Return Types from the CONCAT Function describes.
The following function expression requests the last five
characters of a quoted string:
RIGHT('www.ibm.com',5)
In
this example, the RIGHT function returns the substring m.com