I have a table with a UK Postcode field and I'm going to be testing for bad formats using regular expressions only even rudimentary tests are failing to work.
I have a value of CM8 2AX.
If I run
select post_code from sor.party_person where 'CM8 2AX' ~ '.*[0-9][A-Z][A-Z]$'
and post_code = 'CM8 2AX';
I get
CM8 2AX
(1 row)
Which seems to indicate my very simple regular expression is correct for the value.
Yet if I now test the same value directly from the database - seemingly the same query in effect, having proven with the and condition that this is the value in the field:
select post_code from sor.party_person where post_code ~ '.*[0-9][A-Z][A-Z]$' and post_code = 'CM8 2AX';
(0 rows)
it fails to match.
What am I missing?
If I remove the $ at the end, it does work, implying the db is seeing something different as the end of the field character and not recognising the last text character as being the last character but this makes no sense to me.