Insert a comma in address field before number of house

会有一股神秘感。 提交于 2019-12-02 16:21:05

问题


I have a table with a lot of address field that have no comma between street name and house number. Something like:

"VIA MILANO 123"
"VIA MILANO    A123"
"VIA 11 MILANO      AA123"

What I need is a SQL that insert the comma before the last group that contain numbers... like:

"VIA MILANO, 123"
"VIA MILANO,    A123"
"VIA 11 MILANO,      AA123"

I found something on net but a lot of thinks is not work on IBM DB".

Can anybody help me please ? I have more than 100000 recs in the file to solve.

Thanks in advance Denis


回答1:


Db2 for i 7.2 supports the REGEXP_REPLACE function

https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/db2/rbafzscaregexp_replace.htm

Try this

SELECT c, REGEXP_REPLACE(c,'(\s+\S*\d+\s*$)',',\1') 
FROM TABLE(
    VALUES
     ('VIA MILANO 123     ')
    ,('VIA MILANO A123    ')
    ,('VIA 11 MILANO AA123')
) AS t(c)

returns

 C                      2
 -------------------    --------------------
 VIA MILANO 123         VIA MILANO, 123
 VIA MILANO A123        VIA MILANO, A123
 VIA 11 MILANO AA123    VIA 11 MILANO, AA123

Regular Expressions provide a powerful and flexible way to manipulate strings , and it is worth learning about them.

https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_72/db2/rbafzregexp_like.htm#rbafzregexp_like__regexp_likecontrol



来源:https://stackoverflow.com/questions/57109127/insert-a-comma-in-address-field-before-number-of-house

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