2

Is there a reliable process (using MySQL) to reformat UK postcodes which contain no spaces so that they do?

For example:

  • CM75GJ -> CM7 5GJ
  • L269XR -> L26 9XR
  • YO265TD -> YO26 5TD
xylar
  • 7,433
  • 17
  • 55
  • 100

2 Answers2

2

Looking at UK postal code formatting and assuming there are is no faulty input, this should do the trick:

UPDATE address SET postcode =
    SUBSTRING(postcode, 1, LEN(postcode)-3) + ' ' +
    SUBSTRING(postcode, LEN(postcode)-2, 3)
asontu
  • 4,548
  • 1
  • 21
  • 29
0

For MySQL, this worked for me (Canadian Postal Code):

...
UPPER(CONCAT(SUBSTRING(replace(postal_code, ' ', ''), 1, 3), ' ', SUBSTRING(replace(postal_code, ' ', ''), 4, 3)))
Stephane
  • 11,056
  • 9
  • 41
  • 51