
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
What is the use of LOCATE() function in MySQL?
With the help of LOCATE() function, MySQL returns the position of the first occurrence of a substring in the given string. We must have to pass both the strings (i.e. substring, which is to be searched and the string, from which substring is to be searched) as arguments of the LOCATE() function.
Syntax
LOCATE(Substring, String)
In this function, Substring is the string whose position of occurrence needs to find and the string is a string from which the occurrence of substring needs to be searched.
Example
mysql> Select LOCATE('DE','ABCDEFGH'); +-------------------------+ | LOCATE('DE','ABCDEFGH') | +-------------------------+ | 4 | +-------------------------+ 1 row in set (0.00 sec) mysql> Select LOCATE('G','ABCDEFGH'); +------------------------+ | LOCATE('G','ABCDEFGH') | +------------------------+ | 7 | +------------------------+ 1 row in set (0.00 sec) mysql> Select LOCATE('GH','ABCDEFGH'); +-------------------------+ | LOCATE('GH','ABCDEFGH') | +-------------------------+ | 7 | +-------------------------+ 1 row in set (0.00 sec)
As from the above examples, it can be observed that it returns the first occurrence of the substring within a string.
- Related Articles
- What happens if I will use integer values as arguments of MySQL LOCATE() function?
- What is the use of FIND_IN_SET () function in MySQL?
- What is the use of MySQL FROM_UNIXTIME() function?
- What is the use of MySQL GET_FORMAT() function?
- What is the use of MySQL LAST_INSERT_ID() function?
- What is the use of MySQL CONCAT_WS() function?
- What is the use of MySQL TRUNCATE() function?
- What is the use of MySQL SUBSTRING_INDEX() function?
- What is the use of MySQL UNHEX() function?
- What is the use of MySQL CHAR() function?
- When MySQL LOCATE() function returns NULL as the output?
- What is the use of MySQL IFNULL() control flow function?
- What is the use of MySQL NULLIF() control flow function?
- How can I manage the start position of searching in MySQL LOCATE() function?
- What is the difference between MySQL LOCATE() and FIND_IN_SET() functions?

Advertisements