In Oracle
(select regexp_substr('a,,b','[^,]+', 1, level)
from dual
connect by regexp_substr('a,,b', '[^,]+', 1, level) is not null);
it gives me output
a
b
In Postgresql
select regexp_split_to_table( 'a,,b',',');
it gives me output
a
b
It gives a blank row between a
and b
. Can anyone please suggest how I can get output like Oracle
.
In oracle you were searching for matches, not splitting. Doing the same in Postgres:
SELECT m[1] FROM regexp_matches('a,,b', '[^,]+', 'g') AS T(m);
EDIT: Thanks to a_horse_with_no_name and TimBiegeleisen for making me notice it was returning an array in each row.
For a simple splitting/unnesting like that, string_to_array would be a better choice. Processing regular expressions is quite expensive.
If you put the set returning function into the FROM clause (where it should be) you can filter out the empty rows:
SELECT t.m
FROM unnest(string_to_array('a,,b', ',')) as t(m)
where nullif(trim(t.m),'') is not null;
nullif(trim(tm),'')
will also handle inputs like a, ,b
and treat the empty string as null.
Online example: https://rextester.com/VDWUEM85289
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.