Sub topic 3
INSTR: finding where a substring starts
Assume you know basic SELECT statements, the WHERE clause, VARCHAR2 values, the DUAL table, and the basic meaning of NULL. This chapter uses Oracle SQL examples to show how INSTR locates text inside another string.
What INSTR returns
INSTR searches a character string for a substring. It returns the 1-based position of the first character in the match.
The basic form is:
INSTR(string, substring)For example:
SELECT INSTR('[email protected]', '@') AS at_position,
INSTR('[email protected]', '#') AS missing_position
FROM dual;The result is:
| AT_POSITION | MISSING_POSITION |
|---|---|
| 5 | 0 |
The at sign starts at position 5. The number sign does not occur, so Oracle returns 0. A return value of 0 means “no match.” It is different from NULL.
Positions count characters from left to right, starting at 1:
u s e r @ e x a m p l e . c o m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16INSTR searches for a literal substring. It does not interpret the substring as a regular expression. The search is normally case-sensitive, so INSTR('Oracle', 'o') returns 0 while INSTR('Oracle', 'O') returns 1.
Start position and occurrence
The complete syntax is:
INSTR(string, substring [, position [, occurrence]])The optional position tells Oracle where to begin searching. The optional occurrence tells Oracle which match to return.
With a positive position, Oracle searches from left to right:
SELECT INSTR('a,b,c,d', ',', 1, 2) AS second_comma,
INSTR('a,b,c,d', ',', 1, 3) AS third_comma
FROM dual;The result is:
| SECOND_COMMA | THIRD_COMMA |
|---|---|
| 4 | 6 |
The first comma is at position 2, the second at position 4, and the third at position 6. If the requested occurrence does not exist, INSTR returns 0.
A position greater than 1 skips earlier characters:
SELECT INSTR('a,b,c,d', ',', 3) AS comma_after_position_3
FROM dual;This returns 4. The search begins at character 3, so the comma at position 2 is ignored.
Searching backward
A negative position counts backward from the end of the string. Oracle searches toward the beginning, but the returned position still counts from the beginning.
SELECT INSTR('a,b,c,d', ',', -1) AS last_comma,
INSTR('a,b,c,d', ',', -1, 2) AS second_from_right
FROM dual;The result is:
| LAST_COMMA | SECOND_FROM_RIGHT |
|---|---|
| 6 | 4 |
This is useful when a string contains repeated delimiters. For example, a file name can contain several dots:
SELECT INSTR('report.final.pdf', '.', -1) AS final_dot
FROM dual;The result is 13. Adding 1 gives the starting position of the extension, which can then be used with SUBSTR.
Common errors
Remember these rules:
- Positions start at 1, not 0.
- The occurrence argument starts at 1.
- A missing substring produces 0.
- INSTR returns NULL if its string or substring argument is NULL.
- Oracle treats an empty string literal as NULL.
- Search direction and result numbering are separate. A negative position searches backward, but the returned position is still measured from the left.
- A case difference can prevent a match.
You can use INSTR in a WHERE clause to find rows containing a delimiter:
SELECT email
FROM (
SELECT '[email protected]' AS email FROM dual
UNION ALL
SELECT 'invalid-address' FROM dual
)
WHERE INSTR(email, '@') > 0;This returns only [email protected]. Testing for a position greater than 0 is clearer than testing for a nonzero value because it states what the query needs: a match exists.
Exercise
What does INSTR('a-b-c-d', '-', -1, 2) return?
Show answer
It returns 4. The second hyphen counted from the right is the hyphen at position 4 when positions are counted from the left.