In one table I have a full address which I have to split into the city and postcode to store in other columns.
Here is sample address:
address= 'Marco Polo street 8a, 44000 Vienna'
Some rows may contain multiple addresses but I have to keep only the last portion after the last comma (44000 Vienna).
So the final result should be:
post_office = 44000
city = Vienna
Tried so far: https://stackoverflow.com/a/13430958/2119941
with nested substrings, like this:
city = SUBSTRING(SUBSTRING(address, CHARINDEX(' ', address, 0) + 1, LEN(address)), CHARINDEX(',', address, 0) + 1, LEN(address))
and
post_office = SUBSTRING(SUBSTRING(address, CHARINDEX(',', address, 0) + 1, LEN(address)), 0, CHARINDEX(' ', address, 0))
But this only partially works since cities can also have multiple words like:
city = '44300 Vienna Old Town'
The rule for my data is: find the last comma, take all numbers after the comma and define that as post_office and then, after the first space after post_office until rest of the string it should be defined as the city.
I'm running Microsoft SQL Server 2017 (v14.0.3391.2 - X64)