2

I have a column with address such as '01031 970 São Paulo SP, BR'.

I want to remove spaces between postal codes. The postal code can appear anywhere in the address, e.g. 'São Paulo 01031 970 SP, BR'. The result should be 'São Paulo 01031970 SP, BR' or '01031970 São Paulo SP, BR'

regexp_replace(address, ,'(\s*[0-9]{5}\s+[0-9]{3}\s+)','(\s*[0-9]{5}[0-9]{3}\s+)', 'g')

obviously does not work, but I am looking for an equivalent that does the job.

1 Answer 1

1

Try this query:

update your_table
set address = regexp_replace(address, '([0-9]{5})\s+([0-9]{3})', '\1\2', 'g')
Sign up to request clarification or add additional context in comments.

2 Comments

Can you elaborate on "doesn't work?" I think my regex is correct.
sorry, my mistake. Yes, it indeed works if you leave out the \s* at the beginning and the \s+ at the end (as in your regex). Thank you for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.