How to extract substrings based on specific characters in oracle?
In Oracle database, you can use the SUBSTR function to extract a specific number of characters from a string.
The specific grammar is as follows:
SUBSTR(string, start_position, length)
- String: The string to be extracted.
- start_position: The position at which the extraction should begin. It can be a positive or negative integer. Positive integers indicate counting from the left side of the string, while negative integers indicate counting from the right side of the string.
- Number of characters to be extracted.
Here are some examples:
- Retrieve the first 3 characters from the left side of the string.
SELECT SUBSTR('Hello, World!', 1, 3) FROM dual;
The output is “Hel”
- Retrieve the last 4 characters of a string starting from the right side.
SELECT SUBSTR('Hello, World!', -4, 4) FROM dual;
The output result is “ld!”
- Extract a portion of the string starting from a specified position.
SELECT SUBSTR('Hello, World!', 8, 5) FROM dual;
The output is “World”.