RIGHT(): return rightmost characters from a string

RIGHT() extracts characters from the end of a string. For more flexible positions, see SUBSTRING().

SELECT RIGHT('plus2net.com',3);
Output is com

Syntax
RIGHT(string, LENGTH);
Now Let us try this on our student table on name field
SELECT RIGHT(name,3)  FROM student
We will get right 3 chars of name field of student table.

Collect the domain part from the email address by using RIGHT()
Here is the query
SELECT RIGHT('userid@example.com',(CHAR_LENGTH('userid@example.com') -  LOCATE('@','userid@example.com')))
Output is example.com
In above query we first used two functions, CHAR_LENGTH() to know the total length of the string, then used LOCATE to get the position of the @ in the email address.

By subtracting these two values we get the length of the string present towards right of the @ in the email address. Then we used RIGHT function to get the part of the string towards right of @.

In this query we used @ as the landmark to break the string.
This query is part of several sub quires, for better understanding you can try small parts of the above complex query
SELECT LOCATE('@','userid@example.com')
Output is 7 , this is the position of @ from left side
SELECT CHAR_LENGTH('userid@example.com')
Output is 18, this is the total length of the string
SELECT CHAR_LENGTH(  'userid@example.com' ) - LOCATE(  '@',  'userid@example.com' )
Output is 11, that is the length we require to get the domain part from the email address. Now by using RIGHT function we will get the last 11 chars from the string.
SELECT RIGHT('userid@example.com',(CHAR_LENGTH('userid@example.com') -  LOCATE('@','userid@example.com')))
However we can get better solution by using SUBSTRING function, this we have used in our Project to update domain and userid part from an email address in MySQL table.




Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer