MySQL: LOCATE Function

  • Post author:
  • Post category:MySQL
  • Post comments:1 Comment
MySQL LOCATE Function

In this guide, we will explain how to use the MySQL LOCATE function with syntax and examples.

Description

The MySQL LOCATE function returns the location of the first appearance of a substring in a string.

Syntax

The syntax for the LOCATE function in MySQL is:

LOCATE( substring, string, [start_position ] )

Parameters or Arguments

substring

The substring to search for in string.

string

The string to search.start_positionOptional. The position in string where the search will start. If omitted, it defaults to 1. The first position in the string is 1.

Note

  • The first position in string is 1.
  • If substring is not found in string, then the LOCATE function will return 0.
  • When searching for the location of a substring in a string, the LOCATE function does not perform a case-sensitive search.
  • The POSITION function is a synonym for the LOCATE function.

Applies To

The LOCATE function can be used in the following versions :

  • MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

Example

Let’s look at some LOCATE function examples and explore how to use the LOCATE function.

For example:

mysql> SELECT LOCATE('A', 'adglob.in');
Result: 1

mysql> SELECT LOCATE('a', 'adglob.in');
Result: 1

mysql> SELECT LOCATE('o', 'adglobinfosystem', 2);
Result: 10

mysql> SELECT LOCATE('s', 'adglobinfosystem');
Result: 2

mysql> SELECT LOCATE('d', 'adglobinfosystempvtltd', 2);
Result: 22

mysql> SELECT LOCATE('ob', 'adglob.in', 1);
Result: 5

mysql> SELECT LOCATE('Z', 'adglob.in');
Result: 0

Next Topic : Click Here

This Post Has One Comment

Leave a Reply