-1

I have a MySQL table contains a list of UK address, I was trying to get the list of address doesn't have a postcode.

enter image description here

I the list, we can see some of them don't have postcode at the end.

I was written a query as follows and didn't get the expected result.

select * from property_address WHERE property_address
REGEXP '^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$'

How to fix this query get working?

Shijin TR
  • 7,516
  • 10
  • 55
  • 122

1 Answers1

1

I will assume the postcode regexp is correct.

    REGEXP '^([A-PR-UWYZ0-9]...|GIR 0AA)$'
    ______  _

You need to remove the "anchor" (^) that I underlined above. It is not "not". Instead, negate thus:

NOT REGEXP  '([A-PR-UWYZ0-9]...|GIR 0AA)$'
___         _

Akina's suggestion of first extracting via SUBSTRING_INDEX is likely to make the query faster.

Rick James
  • 135,179
  • 13
  • 127
  • 222