2017-10-15 136 views
1

我想分割由末尾以空格分隔的下面的字符串(存在於單個列中)。對於下面3行,我想以下輸出如何從postgres中的字符串末尾應用split_part函數

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道南COOGEE NSW 2034 AU
12 LISTER STREET WINSTON HILLS NSW 2153 AU

回答

0

當然,這樣的條件解析是不可靠的:

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)