RIGHT String function in MySQL
Let us collect rightmost characters of a string specified by a length property. Syntax
Output is com
Now Let us try this on our student table on name field
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
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
Output is 7 , this is the position of @ from left side
Output is 18, this is the total length of the string
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.
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.
This article is written by plus2net.com team.
▼ More on String