how to apply split_part function from end of string in postgres

雨燕双飞 提交于 2019-12-24 12:19:12

问题


I want to split the below string (present in a single column) separated by spaces from the end. For the below 3 rows, I want the following output

OUTPUT:

Country             STATE             STREET    UNIT
AU                  NSW               2         12
AU                  NSW                         51
AU                  NSW                         12

INPUT:
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


回答1:


of course such conditional parsing is not reliable:

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)


来源:https://stackoverflow.com/questions/46752914/how-to-apply-split-part-function-from-end-of-string-in-postgres

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!