I have an address
column in my table that stores addresses in the following format Street Name, Town Name, Postcode
. I know that I can use the explode
function in PHP and get the postcode based on the commas. My question is, is there a way to extract the postcode using through a query?
Asked
Active
Viewed 1,278 times
2

Gordon Linoff
- 1,242,037
- 58
- 646
- 786

Dimitar Arabadzhiyski
- 272
- 1
- 7
- 21
-
3Edit your question and provide sample data so we can see what the `address` looks like. – Gordon Linoff Jan 29 '18 at 11:28
-
Possible duplicate of [Last index of a given substring in MySQL](https://stackoverflow.com/questions/12775352/last-index-of-a-given-substring-in-mysql) – Kobi Jan 29 '18 at 11:30
1 Answers
4
If your components are separated by commas an always have a postcode, you can use substring_index()
:
select substring_index(address, ',', -1) as postcode

Gordon Linoff
- 1,242,037
- 58
- 646
- 786