I have been struggling with something that should be quite simple for hours now and I would appreciate any advice that could help. I have a Postgres database with addresses, I have a field, building_name which actually contains in many cases, building or apartment numbers. These numbers may or may not be suffixed with a letter e.g. 32A, 24b etc. These combinations could be anywhere in the string including the start or end. They may be followed by whitespace or some other non alphanumeric separator such as a slash or dash. Some examples below:
- '11B' should return '11B'
- 'BURNFOOT COTTAGE' should return nothing as there are no numbers
- '2/1' should return '2'
- '15a' should return '15a'
- '6 CAROLINA COURT' should return '6'
- 'PATRICK THOMAS COURT 83B' should return '83B'
- 'UNIT 51' should return '51'
- '1/6 NEW ASSEMBLY CLOSE' should return '1'
- '15E GREENVALE' should return '15E'
I am trying to achieve this using a regular expression. The closest I can get is '(\d+\w+)' which works for some of the above but does not work for:
'2/1' or '6 CAROLINA COURT' or '1/6 NEW ASSEMBLY CLOSE'
I have followed the advice here SQL split string at first occurance of a number but it does not work for my requirements.
Any advice would be hugely appreciated, I am completely stuck!
Many thanks in advance,
Mark
select substring(building_name from '(\d+\w+)') AS building_num