MySQL LOCATE() Function

LOCATE(substr, str) returns the 1-based position of the first occurrence of a substring. It returns 0 when the substring is absent and NULL if any argument is NULL.

SELECT LOCATE('xy', 'afghytyxyrt');

Output: 8

SELECT LOCATE('z', 'abcdefgh');

Output: 0

Start searching from a position

SELECT LOCATE('xy', 'axyghtxyrt', 4);

Search a table column

SELECT * FROM student WHERE LOCATE('john', name) > 0;

Matching rows:

nameclassmarksex
John DeoFour575male
John Mike Four5 60 male
Alex John Four5 55 male
My John RobFifth5 78male
Big John Four5 55 male
Babby John Four5 69 male

The result depends on the column collation. For ordinary wildcard search, compare this with LIKE.

SELECT LOCATE('john', name) AS position, name
FROM student;

Sample positions:

positionname
1John Deo
0Max Ruin
0Arnold
0Krish Star
1John Mike
4My John Rob

Find @ in an email address

SELECT LOCATE('@', 'myname@example.com');

For a complete sample dataset, see the student table reference. You can combine LOCATE() with SUBSTRING() when a position is needed before extracting part of a string.

Download the student table SQL dump

For pattern-based alternatives, see REGEXP. For broader keyword matching, also see keyword search and SUBSTRING_INDEX().




Subscribe to our YouTube Channel here



plus2net.com
Robin

10-04-2009

I have a table: columns are as follows. min max 0 1300 1301 2000 2001 3900 now user enter something say (700) i want to use a query such that: it only display: min max 0 1300 Please help me
smo

12-04-2009

select * from table where min < 700 and max > 1300 , you can also use Between query
Troy

08-10-2009

In the same vein as your examples above.. suppose that I have a list of names and I want to return all the records where name1 is found a field, then return all records where name2 is found in a field... and so on. I have a list of 375 names and I'm search a single field for the presence of their name.
Hossein

30-01-2010

I have a table with 2 columns as follows: ------------ id code 1 a 2 b 2 c 1 b 3 c 4 a 1 c 2 a ------------ I want to select id's that has all codes a AND b AND c. Please help for sql command. thanks
sql master

10-02-2010

ooopps, this should be the correct code for the query..

select * from tablename where
locate('a', code, 1) <> 0
or locate('b', code, 1) <> 0
or locate('c', code, 1) <> 0
manoj kumar bardhan

07-04-2010

min max 0 1300 query-select * from table1 where min=0 and max=1300
dont know sql

13-12-2012

how can i Find a particular text from all tables in DB.
Prashant Negi

22-06-2013

how can i replace MDH DHANIA POWDER with ABC DHANIA POWDER
rohit

05-07-2014

SELECT * FROM `student` WHERE locate( 'john', name )
-in the above sql query ,only john is given right,
Suppose a column is as follows
aaa dd oii qq
bb ask jiasj sjd
sxd nj kkk

and i want to select record having both bb and ask as substring in the column'. what should i do,how is the query for that
Nishanth

03-11-2014

Can you please help me with a query for the below condition:
To find the position at which the occurance of the string "on" appears the second time in the word "consultation"




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer