Split String by delimiter position using oracle SQL – Sql

Photo of author
Written By M Ibrahim
android-sqlite delimiter node-oracledb split substring

Quick Fix: Utilize REGEXP_SUBSTR and REGEXP_REPLACE functions to split the string based on the position of the delimiter, extracting both parts individually.

The Problem:

Given a string and a delimiter, you want to split the string into two parts at the position of the furthest occurrence of the delimiter. You also want to handle cases where the string contains only one delimiter or no delimiter at all. How can you modify the provided SQL code to achieve this?

The Solutions:

Solution 1: Using REGEXP_SUBSTR() and REGEXP_REPLACE()

To split a string by a delimiter at a certain position, you can use the `REGEXP_SUBSTR()` and `REGEXP_REPLACE()` functions in Oracle SQL. Here’s an improved version of the solution provided:

1. Using REGEXP_SUBSTR() to Extract Parts of the String:

SELECT REGEXP_SUBSTR(val, '[^/]+/[^/]+$', 1, 1) AS part1,
       REGEXP_SUBSTR(val, '[^/]+$', 1, 1) AS part2
FROM (SELECT 'F/P/O' AS val FROM dual) t;

Explanation:

  • The REGEXP_SUBSTR() function is used to extract substrings from the string val based on a regular expression pattern.
  • The regular expression pattern [^/]+/[^/]+$ matches the characters up to the last delimiter (/) followed by the characters after the last delimiter. This ensures that the split occurs at the last delimiter.
  • The 1 in the last argument of REGEXP_SUBSTR() specifies that we want to extract the first match of the regular expression.

2. Using REGEXP_REPLACE() to Handle Cases with Multiple Delimiters:

If your string contains multiple delimiters, you can use the REGEXP_REPLACE() function to remove all but the last delimiter, effectively splitting the string at the last occurrence of the delimiter.

SELECT REGEXP_REPLACE(val, '[^/]+/[^/]+$', '', 1, 1) AS part1,
       REGEXP_SUBSTR(val, '[^/]+$', 1, 1) AS part2
FROM (SELECT 'F/P/O' AS val FROM dual) t;

Explanation:

  • The REGEXP_REPLACE() function replaces the substring that matches the regular expression pattern with an empty string ('').
  • The regular expression pattern [^/]+/[^/]+$ is the same as in the previous example. It matches the characters up to the last delimiter followed by the characters after the last delimiter.
  • The 1 in the last argument of REGEXP_REPLACE() specifies that we want to replace the first match of the regular expression.

Using these functions, you can effectively split a string by the last delimiter and extract the desired parts of the string.

Solution 2: Split string by delimiter position using oracle SQL, using SUBSTR and INSTR

  • SUBSTR and INSTR functions are used to split the string by the furthest delimiter.
  • The negative value in the start_position argument of INSTR indicates that the function should count back from the end of the string.
  • The -1 indicates that the function should start from the last character in the string.
  • The 1 indicates that the function should search for the first occurrence of the delimiter.

For example, consider the following query:

WITH DATA AS
  ( SELECT 'F/P/O' str FROM dual
  )
 SELECT SUBSTR(str, 1, Instr(str, '/', -1, 1) -1) part1,
        SUBSTR(str, Instr(str, '/', -1, 1) +1) part2
 FROM DATA

This query will return the following result:

PART1 PART2
----- -----
F/P   O

In this example, the INSTR function is used to find the position of the last occurrence of the delimiter ‘/’. The SUBSTR function is then used to extract the substring from the beginning of the string up to the position of the delimiter, and the substring from the position of the delimiter to the end of the string.