如何在 postgres 中从字符串末尾应用 split_part 函数
how to apply split_part function from end of string in postgres
我想拆分以下字符串(存在于单个列中),从末尾用空格分隔。对于以下 3 行,我想要以下输出
输出:
Country STATE STREET UNIT
AU NSW 2 12
AU NSW 51
AU NSW 12
输入:
12 2 NOELA PLACE ST MARYS NSW 2760 AU
51 MALABAR ROAD SOUTH COOGEE NSW 2034 AU
12 LISTER STREET WINSTON HILLS NSW 2153 AU
当然这样的条件解析是不靠谱的:
t=# with v(a) as( values('12 2 NOELA PLACE ST MARYS NSW 2760 AU')
,('51 MALABAR ROAD SOUTH COOGEE NSW 2034 AU')
,('12 LISTER STREET WINSTON HILLS NSW 2153 AU')
)
select reverse(split_part(reverse(a),' ',1)), reverse(split_part(reverse(a),' ',3)), case when split_part(a,' ',2) ~ '\d' then split_part(a,' ',2) end st, split_part(a,' ',1) un from v;
reverse | reverse | st | un
---------+---------+----+----
AU | NSW | 2 | 12
AU | NSW | | 51
AU | NSW | | 12
(3 rows)
我想拆分以下字符串(存在于单个列中),从末尾用空格分隔。对于以下 3 行,我想要以下输出
输出:
Country STATE STREET UNIT
AU NSW 2 12
AU NSW 51
AU NSW 12
输入:
12 2 NOELA PLACE ST MARYS NSW 2760 AU
51 MALABAR ROAD SOUTH COOGEE NSW 2034 AU
12 LISTER STREET WINSTON HILLS NSW 2153 AU
当然这样的条件解析是不靠谱的:
t=# with v(a) as( values('12 2 NOELA PLACE ST MARYS NSW 2760 AU')
,('51 MALABAR ROAD SOUTH COOGEE NSW 2034 AU')
,('12 LISTER STREET WINSTON HILLS NSW 2153 AU')
)
select reverse(split_part(reverse(a),' ',1)), reverse(split_part(reverse(a),' ',3)), case when split_part(a,' ',2) ~ '\d' then split_part(a,' ',2) end st, split_part(a,' ',1) un from v;
reverse | reverse | st | un
---------+---------+----+----
AU | NSW | 2 | 12
AU | NSW | | 51
AU | NSW | | 12
(3 rows)