We can specify the number of characters required from left side of a string by using left function in MySQL
We will apply this to our student table on name field
SELECT left(name,5) FROM student
We will get left 5 chars of name field of student table.
Example : Taking out the userid part of email address.
We can use LEFT function to get the userid part of the email address. Before that we will find out the position of @ char inside email address by using locate function. The output of locate function ( which gives the location of @ ) we will use to take the left most characters. Please note that there are several ways to solve this problem and this is one of the way only.
Let us first locate the position of @ in our email address.
SELECT locate('@',email) FROM `newsletter_subscribe`
This will give us a number for each record which we can use to get the userid part.
SELECT left(email,locate('@',email)) FROM `newsletter_subscribe`
This will give us Userid part but will end with one extra @ added to it. Now here is the final one we can get by subtracting 1 from the output of locate function.
SELECT left(email,locate('@',email)-1) FROM `newsletter_subscribe`