2

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?

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • 3
    Edit 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 Answers1

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