Asked 1 years ago
31 May 2022
Views 418
Mahesh Radadiya

Mahesh Radadiya posted

what is the use of LOCATE in MySQL?

what is LOCATE in MySQL ? how to use function LOCATE in MySQL?
sachin

sachin
answered Jun 2 '22 00:00

LOCATE() function can be used to find a string /character in the given string or word in MySQL.
LOCATE() return :
if found in a given string, LOCATE() returns the index in the string where the given character or string is found
if not found in a given string, LOCATE() returns 0

 SELECT LOCATE("r" , "array") AS matcheindex; 

at above query LOCATE try to find "r" character in "array" and it return 2

 SELECT LOCATE("0" IN "array") AS matcheindex; 

at above query LOCATE function try to find "0" character in "array" and it does not found that so it return 0

POSITION() function is Similar to Function LOCATE() in MySQL :
POSITION() function and LOCATE() function are both identical in functionality in MySQL, but they have different syntax.

 SELECT LOCATE("r" ,"array") AS matcheindex; 
 SELECT POSITION("r" IN "array") AS matcheindex; 

above both query return same but they have diffrent syntax LOCATE dont use IN ,it use ,(comma) instead of IN.

LOCATE() function can search case insensitive so it means it finds the given string or character without caring about lowercase or upper case


 SELECT LOCATE("RA" , "array") AS matcheindex; 


so RA is upper case and tries to find in all lower case string "array" but still it finds and returns index.
so in other words:
POSITION("RA" IN "array") is equal to POSITION("ra" IN "array")
Post Answer