Sub topic 2
SUBSTR: Extracting Part of a String
Assume you know basic SELECT statements, VARCHAR2, string literals, the DUAL table, and the basic meaning of NULL. This chapter focuses on how Oracle SQL uses SUBSTR to return part of a character string.
The working model
SUBSTR extracts a substring, which is a portion of a larger string. Its basic syntax is:
SUBSTR(string, start_position [, length])stringis the value to read.start_positionis the 1-based position of the first character to return.lengthis optional. It specifies how many characters to return.- If
lengthis omitted, Oracle returns characters from the start position through the end.
Unlike many programming languages, Oracle counts the first character as position 1, not position 0.
SELECT SUBSTR('Oracle Database', 1, 6) AS first_word,
SUBSTR('Oracle Database', 8) AS rest
FROM dual;The result is:
| FIRST_WORD | REST |
|---|---|
| Oracle | Database |
The space is character 7, so position 8 starts at D.
Choosing a start position and length
The start position identifies where extraction begins. The length controls how far extraction continues.
SELECT SUBSTR('ABCDEFG', 2, 3) AS part_one,
SUBSTR('ABCDEFG', 5, 10) AS part_two
FROM dual;part_one is BCD. part_two is EFG. When the requested length extends beyond the end, Oracle returns the available characters. It does not pad the result to the requested length.
A useful way to predict a result is to mark each character with its position:
String: O r a c l e _ D a t a b a s e
Position: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15For example, SUBSTR('Oracle Database', 8, 4) starts at position 8 and returns positions 8 through 11: Data.
Using SUBSTR with table columns
In a query against a table, replace the string literal with a character column. For example, suppose products.product_code stores values such as ELEC-2026-001.
SELECT product_code,
SUBSTR(product_code, 1, 4) AS category,
SUBSTR(product_code, 6, 4) AS year_code
FROM products;This query assumes that every code follows the same fixed layout. category receives characters 1 through 4, and year_code receives characters 6 through 9.
Fixed positions work well when the format is stable. They become unsafe when values have different lengths or delimiters in different locations. For delimiter-based formats, combine SUBSTR with INSTR, which finds a substring's position. That combination is covered when parsing complete values such as email addresses and names.
Common errors
- Using position 0 as a normal zero-based index: Oracle treats a start position of
0as1. - Forgetting that the length is optional:
SUBSTR(value, 5)means “from position 5 to the end,” not “return five characters.” - Expecting padding: A request longer than the remaining string returns only the remaining characters.
- Passing a nonpositive length: A length of
0or a negative length returnsNULL. - Ignoring
NULL: If the input string or length expression isNULL, the result isNULL.
SELECT SUBSTR('abc', 0, 2) AS position_zero,
SUBSTR('abc', 5) AS past_end,
SUBSTR('abc', 1, 0) AS zero_length
FROM dual;position_zero is ab. past_end and zero_length are NULL.
Exercise
What does SUBSTR('Database', 3, 4) return, and why?
Show answer
It returns taba. Oracle starts at position 3, which is t, and returns four characters: t, a, b, and a.