SQL > SQL String Functions > INSTR Function

The INSTR function in SQL is used to find the starting location of a pattern in a string. This function is available in MySQL and Oracle, though they have slightly different syntaxes:

INSTR returns the 1-based position of a pattern within a string, or 0 if the pattern is not found. Oracle's version offers extra parameters to search from a specific starting position or find the nth occurrence.

Syntax

The syntax for the INSTR function is as follows:

MySQL:

INSTR (str, pattern)

Find the staring location of pattern in string str.

Oracle:

INSTR (str, pattern, [starting position, [nth occurrence]])

Find the starting location of the nth occurrence of pattern beginning in the starting position-th position in string str.

Examples

We use the following table for our examples.

Table Geography

 Region_Name  Store_Name 
 East  Boston 
 East  New York 
 West  Los Angeles 
 West  San Diego 

Example 1 (both Oracle and MySQL)

SELECT INSTR (Store_Name, 'o') 
FROM Geography 
WHERE Store_Name = 'Los Angeles';

Result:

2

The first occurrence of 'o' is the second character in the word 'Los Angeles.'

Example 2 (both Oracle and MySQL)

SELECT INSTR (Store_Name, 'p') 
FROM Geography 
WHERE Store_Name = 'Los Angeles';

Result:

0

In this case, the pattern p does not exist in string 'Los Angeles,' so the function returns 0.

Example 3 (Oracle only)

SELECT INSTR(Store_Name,'e', 1, 2) 
FROM Geography 
WHERE Store_Name = 'Los Angeles';

Result:

10

In this case, we are looking for the second occurrence of the character 'e' in the word 'Los Angeles,' and we start the start with the first character of the word.

The function returns 10 as the second occurrence of 'e' is in the 10th position.

Frequently Asked Questions

Q: What does the SQL INSTR function return?
A: INSTR returns the starting position (1-based index) of the first occurrence of a pattern within a string. If the pattern is not found, it returns 0.

Q: Which databases support INSTR?
A: INSTR is available in MySQL and Oracle. SQL Server uses CHARINDEX() for similar functionality, and PostgreSQL uses POSITION() or STRPOS().

Q: What is the difference between MySQL INSTR and Oracle INSTR?
A: MySQL INSTR(str, pattern) finds the first occurrence only. Oracle INSTR(str, pattern, start_pos, nth_occurrence) additionally lets you specify a starting position and which occurrence to find.

Q: What does INSTR return if the pattern is not found?
A: INSTR returns 0 when the pattern does not exist in the string.

Next: SQL TRIM

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact