Sub topic 6
INSTR with a Start Position, an Occurrence, and Backward Search
Prerequisites and syntax
This chapter assumes that you can write basic SELECT statements, use string literals and VARCHAR2 values, and understand the DUAL table. You should also know that Oracle positions characters from left to right starting at 1.
INSTR finds a substring inside another string. It returns the position of the first character of the requested match.
INSTR(source_string, search_string [, position [, occurrence]])The optional arguments control the search:
| Argument | Meaning | Default |
|---|---|---|
| `source_string` | Text to search | Required |
| `search_string` | Text to find | Required |
| `position` | Where the search begins | `1` |
| `occurrence` | Which match to return | `1` |
A positive position searches from left to right. The returned position always uses the original string's 1-based numbering.
Choosing an occurrence
With the default arguments, INSTR returns the first match. Set occurrence to find a later match.
SELECT INSTR('a,b,c,d', ',', 1, 1) AS first_comma,
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:
| `FIRST_COMMA` | `SECOND_COMMA` | `THIRD_COMMA` |
|---|---|---|
| 2 | 4 | 6 |
The third argument says to begin at position 1. The fourth argument says which comma to return. Oracle counts only matching substrings, not every character scanned.
A search can begin later in the string. This is useful when an earlier delimiter should be ignored.
SELECT INSTR('a,b,c,d', ',', 3, 1) AS first_comma_after_position_3,
INSTR('a,b,c,d', ',', 3, 2) AS second_comma_after_position_3
FROM dual;The first result is 4, and the second result is 6. The comma at position 2 is before the starting position, so it is not considered.
Searching backward
A negative position starts at a position counted from the end and searches toward the beginning. For example, -1 starts at the final character.
SELECT INSTR('a,b,c,d', ',', -1, 1) AS last_comma,
INSTR('a,b,c,d', ',', -1, 2) AS second_last_comma
FROM dual;The result is:
| `LAST_COMMA` | `SECOND_LAST_COMMA` |
|---|---|
| 6 | 4 |
The first backward match is the comma at position 6. The second backward match is the comma at position 4. The returned values refer to positions in the original left-to-right string, so they are not negative.
A backward search is especially useful for file names, paths, and versioned identifiers. To find the final dot in a file name, search backward from the end:
SELECT INSTR('report.final.pdf', '.', -1, 1) AS final_dot,
SUBSTR('report.final.pdf',
INSTR('report.final.pdf', '.', -1, 1) + 1) AS extension
FROM dual;final_dot is 13, and extension is pdf. The SUBSTR expression uses the last dot's position to extract everything after it.
Missing matches and common errors
If the requested occurrence does not exist, INSTR returns 0, not NULL.
SELECT INSTR('a,b,c', ',', 1, 5) AS missing_occurrence,
INSTR('a,b,c', '#', 1, 1) AS missing_character
FROM dual;Both results are 0. This matters when the result is used in arithmetic. For example, adding 1 to a missing match produces 1, which may not represent a valid delimiter position. Check for 0 before using the result to split text.
Another common error is confusing the starting position with the occurrence number. In INSTR('a,b,c,d', ',', 3, 2), 3 is where the scan begins and 2 means the second matching comma after that point.
Exercise
What does INSTR('one.two.three', '.', -1, 2) return?
Show answer
It returns 7. The first backward dot is at position 9, and the second backward dot is at position 7 in the original string.