问题
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