How to Use the LOCATE and POSITION Functions in MySQL

Source: Internet
Author: User
Not commonly used: the LOCATE and POSITION Functions in MySQL

Not commonly used: the LOCATE and POSITION Functions in MySQL

LOCATE (substr, str)
POSITION (substr IN str)
Returns the position of the substring substr that appears for the first time in the str string. If the substring substr does not exist in str, the return value is 0:
Mysql> select locate ('bar', 'foobarbar ');
-> 4
Mysql> select locate ('xbar', 'foobar ');
-> 0

This function is multi-byte secure. In MySQL 3.23, this function is case-sensitive. in MySQL 4.0, if any parameter is a binary string, it is case-sensitive.

LOCATE (substr, str, pos)
Returns the position where the substring substr first appears after the position pos in the str string. If substr is not in str, return 0:
Mysql> select locate ('bar', 'foobarbarbar ', 5 );
-> 7

This function is multi-byte secure. In MySQL 3.23, this function is case-sensitive. in MySQL 4.0, if any parameter is a binary string, it is case-sensitive.

A typical example is:
Slect * from tablename where LOCATE (colum1, colum1, 1)> 0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.