LEFT() string function in MySQL

LEFT() extracts characters from the start of a string. For the opposite end, see RIGHT().

SELECT LEFT('this is a test',7)
The output of this is here
this is
Use LEFT() to return the requested number of characters from the beginning of a string.

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`





Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










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