

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 UNHEX() function?
- What is the use of MySQL SUBSTRING_INDEX() function?
- What is the use of MySQL CHAR() function?
- What is the use of FIND_IN_SET () function in MySQL?
- What happens if I will use integer values as arguments of MySQL LOCATE() function?
- What is the use of MySQL NULLIF() control flow function?
- What is the use of MySQL IFNULL() control flow function?
- What is the difference between MySQL LOCATE() and FIND_IN_SET() functions?
- When MySQL LOCATE() function returns NULL as the output?
- How can I manage the start position of searching in MySQL LOCATE() function?
Advertisements