The INSTR function in Oracle SQL is an essential tool for finding the position of a substring within another string. It's a powerful function that enables users to retrieve characters from strings based on their occurrence, making it a fundamental part of string manipulation in Oracle. Let's delve into this function, its syntax, and some practical examples to help you harness its capabilities.

Before we dive into the intricacies of the INSTR function, let's recap the basics of string manipulation in Oracle SQL. In Oracle, strings are manipulated using various built-in functions, with the INSTR function being one of the most popular for locating substrings within larger strings.

The INSTR Function: Syntax and Basic Usage
The INSTR function in Oracle SQL follows a simple and intuitive syntax. It takes two main parameters: the string (or expression) you're searching in and the substring you're searching for. Here's the basic syntax:

INSTR(string, substring)
The function returns the position of the first occurrence of the substring in the string. If the substring is not found, the function returns 0.

Finding the Position of a Substring
Let's start with a simple example where we find the position of the substring 'oracle' in the string 'Oracle Database' using the INSTR function:
SELECT INSTR('Oracle Database', 'oracle') FROM DUAL;

The INSTR function will return '8', indicating that the first character of the substring 'oracle' is the 8th character in the string 'Oracle Database'.
Case Sensitivity and Ignoring Whitespace
The INSTR function is case-sensitive by default. If you want to perform a case-insensitive search, you can use the INSTRI function. Similarly, to ignore starting whitespace, you can use the INSTRB function. For instance, the following query finds the first occurrence of the substring 'oracle' in the string ' Oracle Database', ignoring the leading whitespace:

SELECT INSTRB(' Oracle Database', 'oracle') FROM DUAL;
Advanced INSTR Function Usage



















While the basic INSTR function is quite powerful, Oracle also allows you to specify starting positions for the search and retrieve multiple occurrences of the substring.
Specifying a Starting Position
You can specify a starting position for the search in the string by including a third parameter, 'position', in the INSTR function. The following query finds the position of the second occurrence of the substring 'a' in the string 'banana':
SELECT INSTR('banana', 'a', 2) FROM DUAL;
Retrieving Multiple Occurrences
To retrieve multiple occurrences of a substring, you can use a loop to repeatedly call the INSTR function, incrementing the starting position each time. However, a more efficient way is to use the CONNECT BY LEVEL trick, demonstrated in the following example:
SELECT INSTR('banana', 'a', lvl) pos
FROM dual
CONNECT BY level <= REGEXP_COUNT('banana', 'a');
This query returns the position of each occurrence of 'a' in the string 'banana'.
In Oracle SQL, the INSTR function is indispensable for string manipulation. Whether you're finding the position of a substring, ignoring case or leading whitespace, or retrieving multiple occurrences, the INSTR function offers a straightforward and efficient solution. With practice, you'll find that the INSTR function becomes an invaluable tool in your Oracle SQL toolkit, helping you extract exactly the data you need from strings.