Sub topic 5
Counting from the End with Negative Positions
Prerequisites and the basic rule
This chapter assumes that you can write basic SELECT statements, use the DUAL table, and distinguish a NULL value from an empty string. You should also know that Oracle treats an empty string assigned to a character column as NULL.
SUBSTR extracts part of a string. Its second argument is the starting position. A positive position counts from the beginning, while a negative position counts backward from the end.
After Oracle finds the starting point, it extracts characters from left to right. A negative position does not make the extraction run backward.
SELECT SUBSTR('Oracle Database', -8) AS last_eight,
SUBSTR('Oracle Database', -8, 4) AS first_four
FROM dual;The string has 15 characters. Position -1 is the final e in Database, and position -8 is the D. The result is:
| LAST_EIGHT | FIRST_FOUR |
|---|---|
| Database | Data |
The optional third argument is the number of characters to return. Without it, Oracle returns characters from the calculated starting point through the end of the string.
Mapping negative positions to ordinary positions
You can reason about a negative position by converting it to a positive position:
positive position = string length + negative position + 1For Oracle Database, which has length 15:
-1maps to position15-2maps to position14-8maps to position8
This makes SUBSTR('Oracle Database', -8, 4) equivalent to SUBSTR('Oracle Database', 8, 4).
The following query shows several useful suffix operations:
SELECT SUBSTR('report.final.pdf', -3) AS extension,
SUBSTR('report.final.pdf', -7) AS suffix,
SUBSTR('report.final.pdf', -1, 1) AS final_character
FROM dual;The results are pdf, l.pdf, and f. Use a negative position when the part you need is defined relative to the end, such as a file extension, a country code, or the last few characters of an identifier.
A visual model
Negative positions with INSTR
INSTR also accepts a negative starting position. In that function, the negative position changes the search direction. Oracle begins near the end and searches backward for the requested occurrence.
This is useful when a string contains several separators. For example, a filename can contain multiple dots:
SELECT INSTR('report.final.pdf', '.', -1) AS last_dot,
SUBSTR('report.final.pdf',
INSTR('report.final.pdf', '.', -1) + 1) AS extension
FROM dual;INSTR returns 13, the position of the final dot. Adding 1 skips that dot, and SUBSTR returns pdf.
This differs from SUBSTR:
| Function | Meaning of a negative position |
|---|---|
| `SUBSTR` | Count backward to choose the extraction start |
| `INSTR` | Begin searching backward for a match |
Boundaries and common mistakes
A start position of zero is treated as one. A length of zero or a negative length returns NULL. A start position beyond the usable string range also produces NULL.
SELECT SUBSTR('abc', -1, 1) AS last_character,
SUBSTR('abc', -3, 2) AS from_first,
SUBSTR('abc', -5, 2) AS outside_range,
SUBSTR('abc', 1, -1) AS bad_length
FROM dual;The first two expressions return c and ab. The last two return NULL. Remember that Oracle displays NULL as an empty-looking field in many query tools, so inspect the expression carefully rather than assuming it returned an empty string.
A NULL input produces NULL:
SELECT SUBSTR(CAST(NULL AS VARCHAR2(20)), -2) AS result
FROM dual;Do not confuse a negative position with a negative length. The position may be negative because it identifies a location from the end. The length must still be positive when supplied.
Exercise
What does SUBSTR('abcdef', -4, 2) return, and why?
Show answer
It returns 'cd'. The string length is 6, so position -4 maps to position 3. SUBSTR then takes two characters from position 3.