SELECT RIGHT('plus2net.com',3);
Output is comRIGHT(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.
SELECT RIGHT('userid@example.com',(char_length('userid@example.com') - LOCATE('@','userid@example.com')))
Output is example.comSELECT 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.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.