All we need is an easy explanation of the problem, so here it is.

I migrate from Oracle DB to Postgres/MS-SQL and I want to use REGEXP_SUBSTR equivalent with these databases. I need to rewrite this statement to these databases:

       REGEXP_SUBSTR(b.description, '(st|ek)', 1, 1, NULL, 1) substring
FROM books b;

I was able to write this statement for PostgreSQL as:

       (SELECT array_to_string(a, '') FROM REGEXP_MATCHES(b.description, '(st|ek) ', 'g') AS f(a) LIMIT 1) substring
FROM books b;

However I have no idea how to rewrite to MS-SQL db. I tried to used statement below however bad result is returned for substring column. Is any solution for this issue please?

           (SELECT SUBSTRING(b.description, PATINDEX('(st|ek)', CAST(b.description AS varchar)), LEN(b.description))) substring
 FROM books b;

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

The solution for Postgres is to use substring()

substring('one two three four five' from '(\S+)')

yields one. The biggest difference to Oracle’s regexp_substring() is that you can’t locate the "nth" occurance directly. To get the second word, you could use:

substring('one two three four five' from '\S+\s*(\S+)')

If you are trying to access words (separated by whitespace), then splitting the string into an array is another alternative:

(regexp_split_to_array('one two three four five', '\s+'))[1]

The above returns the first word, [2] would return the second. The regex defines the separator (delimiter) between each element.

Note that Postgres 15 will have a regexp_substr() that is pretty much identical to Oracle’s implementation.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply