0
0
MySQLquery~10 mins

LOCATE and INSTR in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - LOCATE and INSTR
Start with string and substring
Search substring in string
Find first occurrence position
Return position or 0 if not found
End
LOCATE and INSTR find the position of a substring inside a string and return its first occurrence index or 0 if not found.
Execution Sample
MySQL
SELECT LOCATE('cat', 'concatenate');
SELECT INSTR('concatenate', 'cat');
Find the position of 'cat' inside 'concatenate' using LOCATE and INSTR functions.
Execution Table
StepFunctionInput StringSubstringPosition FoundOutput
1LOCATE'concatenate''cat'44
2INSTR'concatenate''cat'44
3LOCATE'hello world''cat'00
4INSTR'hello world''cat'00
5LOCATE'banana''na'33
6INSTR'banana''na'33
7LOCATE'banana''na' (start=4)55
8INSTR'banana''na'33
9LOCATE'apple''p'22
10INSTR'apple''p'22
11LOCATE'apple''z'00
12INSTR'apple''z'00
💡 All searches complete; positions returned or 0 if substring not found.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6
Input String'concatenate''concatenate''hello world''hello world''banana''banana''banana'
Substring'cat''cat''cat''cat''na''na''na'
Position Foundnull400333
Outputnull400333
Key Moments - 3 Insights
Why does LOCATE return 0 when the substring is not found?
LOCATE returns 0 to indicate the substring does not exist in the string, as shown in execution_table rows 3 and 4.
What is the difference between LOCATE and INSTR in usage?
LOCATE allows an optional start position parameter (see row 7), while INSTR always searches from the start. Both return the first occurrence position.
Why does LOCATE('na', 'banana', 4) return 5 instead of 3?
Because LOCATE starts searching from position 4, it finds the next 'na' at position 5, as shown in execution_table row 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 1. What position does LOCATE return for 'cat' in 'concatenate'?
A4
B3
C0
D5
💡 Hint
Check the 'Position Found' and 'Output' columns in row 1.
At which step does LOCATE start searching from a position other than 1?
AStep 2
BStep 7
CStep 9
DStep 11
💡 Hint
Look for the row where LOCATE has a third parameter (start position).
If the substring is not found, what do LOCATE and INSTR return according to the table?
ANULL
B-1
C0
D1
💡 Hint
See rows 3, 4, 11, and 12 for outputs when substring is missing.
Concept Snapshot
LOCATE(substring, string[, start]) and INSTR(string, substring) find the first position of substring in string.
They return the position as a number starting at 1.
If substring not found, they return 0.
LOCATE can start searching from a given position; INSTR always starts at 1.
Useful for finding where text appears inside other text.
Full Transcript
LOCATE and INSTR are MySQL functions to find the position of a substring inside a string. LOCATE takes the substring first, then the string, and optionally a start position to begin searching. INSTR takes the string first, then the substring, and always searches from the start. Both return the position of the first occurrence starting at 1, or 0 if the substring is not found. For example, LOCATE('cat', 'concatenate') returns 4 because 'cat' starts at the 4th character. If the substring is missing, like LOCATE('cat', 'hello world'), it returns 0. LOCATE can also start searching from a specific position, like LOCATE('na', 'banana', 4) returns 5, finding the second 'na'. These functions help find where text appears inside other text strings.