MySQL numeric functions
MySQL string functions
MySQL Date/Time functions
MySQL aggregate functions
MySQL flow control functions
The RIGHT()
function in MySQL is a string function that extracts a specified number of characters from the right end of a given string.
Here's the syntax:
RIGHT(string, length);
string
: the string from which you want to extract characters.length
: the number of characters to extract from the right end of the string.For instance, if you want to extract the last 3 characters from the string 'HelloWorld', you can use the RIGHT()
function as follows:
SELECT RIGHT('HelloWorld', 3);
This will return: 'rld'
You can also use the RIGHT()
function with columns in a table. For example, let's say you have a table users
with a phone_number
column, and you want to get the last four digits of each phone number. You can do it like this:
SELECT phone_number, RIGHT(phone_number, 4) as last_four_digits FROM users;
This query will return a result set with the original phone number and the last four digits for each row in the users
table.
As with other MySQL functions, you can combine RIGHT()
with other functions for more complex operations. For example, to count the number of '9' characters in the last four digits, you could use:
SELECT phone_number, LENGTH(RIGHT(phone_number, 4)) - LENGTH(REPLACE(RIGHT(phone_number, 4), '9', '')) as count_9 FROM users;
This query first extracts the last four digits of the phone_number
, then counts the number of '9' characters in those digits for each row in the users
table.
Remember, the RIGHT()
function does not change the original data in the table. It only returns the extracted string as a result of the query.
Truncating strings from the right with MySQL RIGHT:
SELECT RIGHT('Hello, World!', 7) AS TruncatedString;
This query selects the rightmost 7 characters from the string 'Hello, World!', resulting in 'World!'.
Using RIGHT function in MySQL for string truncation:
SELECT RIGHT('MySQL RIGHT function example', 8) AS TruncatedString;
This query uses the RIGHT
function to extract the rightmost 8 characters from the given string.
MySQL RIGHT function example:
SELECT RIGHT('MySQL is powerful', 8) AS TruncatedString;
The RIGHT
function is applied to the string 'MySQL is powerful', returning the rightmost 8 characters.
How to truncate a string from the right in MySQL:
SELECT RIGHT('Truncate this string', 10) AS TruncatedString;
This query truncates the string 'Truncate this string' from the right, keeping only the rightmost 10 characters.
String manipulation in MySQL: RIGHT usage:
SELECT RIGHT('String manipulation with MySQL', 5) AS TruncatedString;
Demonstrating the use of the RIGHT
function in MySQL for string manipulation, this query extracts the rightmost 5 characters.
MySQL string truncation with RIGHT:
SELECT RIGHT('String to be truncated in MySQL', 12) AS TruncatedString;
Using the RIGHT
function, this query truncates the string from the right, keeping only the rightmost 12 characters.
Truncating text in MySQL using RIGHT function:
SELECT RIGHT('Text to be truncated in MySQL', 15) AS TruncatedText;
The RIGHT
function is used to truncate the text from the right, preserving only the rightmost 15 characters.
Text truncation with RIGHT in MySQL:
SELECT RIGHT('Text truncation example in MySQL', 10) AS TruncatedText;
This query showcases text truncation using the RIGHT
function, extracting the rightmost 10 characters.