Skip to content

Conversation

@grega
Copy link
Member

@grega grega commented Jul 24, 2024

Context
UK postcode data is used in our reporting tools in order to measure how effective we are at reaching deprived areas (using the IMD / Indices of Multiple Deprivation: https://www.gov.uk/government/collections/english-indices-of-deprivation). This is a key part our programme impact monitoring work.

The IMD lookup relies on a correctly formatted postcode as defined by the NSPL (National Statistics Postcode Lookup) data: 2-4 char outward code, a space, then a 3-char inward code.

Problem
Postcode validation (as in "is this an actual UK postcode that is definitely in the Royal Mail Postcode Address File / PAF") is suboptimal, as doing so with a regex is a pain and potentially error prone (with the onus to 'fix' a potentially valid postcode being put on the user). We're also currently discounting using an API to look up / validate postcodes, though this may be a valid approach going forwards.

Proposed approach
Whilst we don't want to take a heavy-handed approach to postcode validation, as outlined above, we do want to make sure that any postcode that meets the criteria of being "very likely to be in the PAF and NSPL data sets" is formatted in a way which makes lookups possible (in order to prevent cases such as CB11NT, CB 11NT):

  • Take any >=5 char UK postcode and ensure it has one space in it before the final 3 chars / inward code.

Additional steps
Existing school data will need to have the same transform / corrections applied, something like:

UPDATE schools
SET postal_code = 
    CASE 
        WHEN LENGTH(postal_code) >= 5 AND POSITION(' ' IN postal_code) = 0
        THEN CONCAT(SUBSTRING(postal_code FROM 1 FOR LENGTH(postal_code) - 3), ' ', SUBSTRING(postal_code FROM LENGTH(postal_code) - 2))
        ELSE postal_code
    END
WHERE country_code = 'GB';

@cla-bot cla-bot bot added the cla-signed label Jul 24, 2024
@grega grega temporarily deployed to editor-api-p-format-uk--dhwvkm July 24, 2024 12:14 Inactive
Copy link
Contributor

@sra405 sra405 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍 NICEEEE!! Love a little code snatching from other apps 😅

@grega grega temporarily deployed to editor-api-p-format-uk--dhwvkm July 24, 2024 12:48 Inactive
@grega grega merged commit a7f49d2 into main Jul 24, 2024
@grega grega deleted the format-uk-postcodes branch July 24, 2024 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants