- Categories:
String & binary functions (Matching/Comparison)
RIGHT¶
Returns a rightmost substring of its input.
RIGHT(STR, N) is equivalent to SUBSTR(STR, LENGTH(STR)-N+1, N).
- See also:
Syntax¶
Arguments¶
string_exprAn expression that evaluates to a VARCHAR or BINARY value.
length_exprAn expression that evaluates to an integer. It specifies:
The number of UTF-8 characters to return if the input is a VARCHAR value.
The number of bytes to return if the input is a BINARY value.
Specify a length that is greater than or equal to zero. If the length is a negative number, the function returns an empty string.
Returns¶
The data type of the returned value is the same as the data type of the string_expr (VARCHAR or BINARY).
If any of the inputs are NULL, NULL is returned.
Usage notes¶
If length_expr is greater than the length of expr, then the function returns expr.
Collation details¶
Collation applies to VARCHAR inputs. Collation doesn’t apply if the input data type of the first parameter is BINARY.
No impact. Although collation is accepted syntactically, collations don’t affect processing. For example, two-character and three-character letters in languages (for example, “dzs” in Hungarian or “ch” in Czech) are still counted as two or three characters (not one character) for the length argument.
The collation of the result is the same as the collation of the input. This can be useful if the returned value is passed to another function as part of nested function calls.
Examples¶
The following examples use the RIGHT function.
Basic example¶
Returning substrings for email, phone, and date strings¶
The following examples return substrings for customer information in a table.
Create the table and insert data:
Use the LENGTH and POSITION functions with the RIGHT function to extract the domains from
email addresses. This example first finds the length of the input string and then subtracts the position
of @ in each string to determine the length of the domain:
Tip
You can use the POSITION function to find the position of other characters, such as an empty
character (' ') or an underscore (_).
In the cust_phone column in the table, the area code is always the first three characters. Extract
the phone numbers without the area codes:
In the activation_date column in the table, the date is always in the format YYYYMMDD. Extract day from
these strings: