Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

MySQL LOCATE Function

by suresh

The MySQL Locate function is one of the MySQL String Functions, which is useful to find the first occurrence of a substring in a string expression and returns the index value.

Let me show you how to write String Locate in MySQL to find the index position with an example.

MySQL String Locate Syntax

The basic syntax of string Locate in MySQL is as shown below:

LOCATE(Substring, String)

LOCATE(Substring, String, Start_Position)

This Locate function returns the index position of the first occurrence of a substring. If we provided the Start_Position, then this function starts looking from that index position and returns the first occurrence after that position.

MySQL Locate Function Example 1

The String Locate Function returns the index position. The following query shows multiple ways to use this String Locate function.

SELECT LOCATE('l', 'Hello');

SELECT LOCATE('o', 'Helloworld');

SELECT LOCATE('j', 'Helloworld');

OUTPUT

MySQL LOCATE Function 1

In the example, we are using the Start Position of a MySQL String Locate function. The Second MySQL statement starts looking from the 10th position, and the third statement starts looking from the 26th position.

SELECT LOCATE('abc', 'This is abc working in abc Company');

SELECT LOCATE('abc', 'This is abc working in abc Company', 10);

SELECT LOCATE('abc', 'This is abc working in abc Company', 26);

OUTPUT

MySQL LOCATE Function 2

MySQL Locate String Example 2

In this example, we implement this MySQL string locate function on different columns present in the employe table.

The following String Function statement finds or locates the first occurrence of @, com, and mail words from the Email column.

SELECT FirstName,
LastName,
      DepartmentName,
      Email,
      LOCATE('@', Email),
      LOCATE('com', Email),
      LOCATE('mail', Email)
FROM `MySQL Tutorial`.employe;

OUTPUT

MySQL LOCATE Function 3
=

Placed Under: MySQL

  • How to Download MySQL
  • Install MySQL on Windows
  • MySQL Create Database
  • MySQL Delete Database
  • MySQL Create Table
  • MySQL Drop Table
  • MySQL SELECT Statement
  • MySQL ALIAS Column
  • MySQL Distinct
  • MySQL Insert Statement
  • MySQL Delete
  • MySQL Truncate Table
  • MySQL WHERE Clause
  • MySQL Order By
  • MySQL Group By
  • MySQL Having Clause
  • MySQL LIMIT
  • MySQL Arithmetic Operators
  • MySQL COALESCE Function
  • MySQL AND Operator
  • MySQL NOT Operator
  • MySQL OR Operator
  • MySQL XOR Operator
  • MySQL BETWEEN Operator
  • MySQL Not Between Operator
  • MySQL GREATEST Function
  • MYSQL LEAST Function
  • MySQL LIKE Operator
  • MySQL NOT LIKE Operator
  • MySQL IFNULL Operator
  • MySQL NULLIF Operator
  • MySQL INTERVAL Operator
  • MySQL IS Operator
  • MySQL IN Operator
  • MySQL NOT IN Operator
  • MySQL IS NOT NULL
  • MySQL IS NULL
  • MySQL Inner Join
  • MySQL Cross Join
  • MySQL Right Join
  • MySQL Left Join
  • MySQL Aggregate Functions
  • MySQL Date Functions
  • MySQL Date Function
  • MySQL String Functions
  • MySQL Numeric Functions
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy