[英]Convert Query Oracle to Postgresql
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 在PostgreSQL中
select regexp_split_to_table( 'a,,b',',');
it gives me output 它给我输出
a
b
It gives a blank row between a
and b
. 它在
a
和b
之间给出空白行。 Can anyone please suggest how I can get output like Oracle
. 任何人都可以建议我如何获得
Oracle
这样的输出。
In oracle you were searching for matches, not splitting. 在oracle中,您正在搜索匹配项,而不是拆分。 Doing the same in Postgres:
在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. 编辑:感谢a_horse_with_no_name和TimBiegeleisen使我注意到它正在每行中返回一个数组。
For a simple splitting/unnesting like that, string_to_array would be a better choice. 对于像这样的简单拆分/取消嵌套,string_to_array是一个更好的选择。 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: 如果将集合返回函数放在FROM子句中(应该放在其中),则可以过滤出空行:
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. nullif(trim(tm),'')
还将处理输入a, ,b
,并将空字符串视为null。
Online example: https://rextester.com/VDWUEM85289 在线示例: https : //rextester.com/VDWUEM85289
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.