RIGHT() : collect rightmost characters of a string specified by a length

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

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_legth() 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.
SQL String References elt(): String from the postion substring_index to get part of string using delimiter FIND_IN_SET

Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net


    Post your comments , suggestion , error , requirements etc here

    SQL Video Tutorials

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