SUBSTR and INSTR SQL Oracle
Understanding Oracle’s String Functions
Oracle provides several string functions to manipulate and extract data from strings. Two of these functions, SUBSTR and INSTR, are commonly used in database queries. In this article, we will delve into the world of Oracle’s string functions and explore how they work.
Introduction to SUBSTR
The SUBSTR function is used to extract a specified number of characters from a string. It returns the substring starting at a specific position, with a specified length. The syntax for the SUBSTR function in Oracle is:
SUBSTR (expression, start_position [ , length ])
Where:
expressionis the string from which to extract characters.start_positionis the position where you want to begin extracting characters. Positions are 1-based, meaning the first character of a string is at position 1.lengthis the number of characters to extract.
Understanding INSTR
The INSTR function returns the position of a specified character within a string. It returns 0 if the specified character is not found in the string. The syntax for the INSTR function in Oracle is:
INSTR (expression, pattern [ , start_position ])
Where:
expressionis the string in which to find characters.patternis the character or substring you want to find.start_positionis the position at which to begin searching for the specified character. Positions are 1-based.
The Example Query
The query provided in the Stack Overflow question:
SELECT PHONE, SUBSTR(PHONE, 1, INSTR(PHONE, '-') -1)
FROM DIRECTORY;
At first glance, this query seems confusing because it returns a value of 362, which doesn’t seem to make sense given the original string value of 362-127-4285.
Breaking Down the Query
To understand why the query returns the expected result, let’s break down how INSTR and SUBSTR work together:
Finding the Position of
-:The
INSTR(PHONE, '-')function finds the position of the first occurrence of'-'in the string.In this case, since
'-'is at the second character (index 2) of the original string (362-127-4285),INSTR(PHONE, '-')returns a value of2.INSTR('362-127-4285', '-') = 2The
-1in the query is used to get the position without the length of the substring. In Oracle’sINSTRfunction, if you omit thelength, it returns the starting position.INSTR('362-127-4285', '-') = 2 INSTR(PHONE, '-') -1 = 2 - 1 = 1The
INSTRfunction with-1returns the character at index1. In this case, it’s'3'.INSTR('362-127-4285', '-') = 2 SUBSTR(PHONE, 1, INSTR(PHONE, '-') -1) = SUBSTR('362-127-4285', 1, 1) = '3' ```
Extracting the Substring:
The
SUBSTRfunction extracts a substring starting at position1, with a length of3.Since we want to extract characters up until but not including the next occurrence of
'-', theINSTRvalue of1is used as both the start and end positions for theSUBSTR.SUBSTR(PHONE, 1, INSTR(PHONE, '-') -1) = SUBSTR('362-127-4285', 1, 3) = '362' ```
In summary, the query returns 362 because:
- The
INSTRfunction finds the position of'-'in the string and subtracts1from it to get the starting position of the substring. - The
SUBSTRfunction extracts a substring starting at position1, with a length of3. - Both functions work together to extract the substring without including any characters after the first occurrence of
'-'.
Conclusion
The provided query may seem counterintuitive at first, but by breaking down how INSTR and SUBSTR work, we can see that it’s actually extracting a meaningful substring from the original string.
When working with strings in Oracle, understanding these functions is crucial for accurately manipulating and analyzing data. By using INSTR to find positions and SUBSTR to extract substrings, you can perform complex operations on character data.
Last modified on 2023-07-21