RIGHT String function in MySQL

Let us collect rightmost characters of a string specified by a length property. Syntax
RIGHT(string, length);
Example
SELECT RIGHT('plus2net.com',3);
Output is com

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.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Your Rating


Google+

Post Comment This is for short comments only. Use the forum for more discussions.




Subscribe to our mailing list

* indicates required
Subscribe to plus2net




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer