left String function in MySQL

We can specify the number of characters required from left side of a string by using left function in MySQL Let us start with an easy example
SELECT left('this is a test',7)
The output of this is here
this is
Now 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`


* indicates required
Subscribe to plus2net


    Post your comments , suggestion , error , requirements etc here .

    SQL Video Tutorials

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