Web design and hosting, database, cloud and social media solutions that deliver business results
  • Business Solutions
    • Robotic Process Automation
    • Software
    • Database Consultancy Services
      • Data Integration
      • Datawarehouse Services
      • Power BI
      • Server Upgrade and DBA Services
    • Web Site Design Services
      • Logo Design
      • Payment Gateways
      • Web Localisation and Translation
      • Web Site Optimisation
      • Web Site Security
      • Technical Tools
    • Cloud Services
      • Amazon Web Services
      • Google Cloud Services
      • Microsoft Azure
    • Microsoft Office
    • Social Media Management and Advice Services
  • Academy
    • Our Test Environment
    • Learning Databases
      • The Basics
      • Get Open Query
      • SQL Server Data
      • SQL Server Maintenance
      • Using SQL Server Dates
      • Using SQL Server Functions
      • Using SQL Server Pivot-Unpivot
      • Technical Tools
    • Learning Web Design
      • Building Ousia Content Management System
      • Using ASP-NET
      • Using CSS
      • Using JavaScript
    • Learning Cloud and IT Services
      • Task Scheduler Error 2147943645
      • Requesting SSL and Generation of PFX file in OpenSSL Simple Steps
    • Using Social Media
      • Asking for a Google Review
      • Changing a Facebook account from personal to business
      • Choosing where to focus Social Media effort
      • Social Media Image Sizes
      • Using Meta Data to set Social Media Images
  • About Us
    • Blog
      • Building an entry level gaming machine
      • Google Core Update Jan 2020
      • Hot Chilli Internet Closure
      • How To Choose Content For Your Website Adverts Leaflets
      • Preventing Online Scam
      • Skimmers of the gig economy
      • The most annoying things about websites on the Internet
      • Top 5 websites for free Vector Graphics
    • Careers
      • Translator English-Portuguese
      • Translator English-Spanish
    • Portfolio
    • Team
      • Adrian Anandan
      • Ali Al Amine
      • Ayse Hur
      • Chester Copperpot
      • Deepika Bandaru
      • Gavin Clayton
      • Sai Gangu
      • Suneel Kumar
      • Surya Mukkamala
عربى (AR)čeština (CS)Deutsch (DE)English (EN-US)English (EN-GB)Español (ES)فارسی (FA)Français (FR)हिंदी (HI)italiano (IT)日本語 (JA)polski (PL)Português (PT)русский (RU)Türk (TR)中国的 (ZH)

Cleansing of UK Address Postcodes in SQL Server 2008

Create an SQL Server Function to cleanse, replace common mistakes and re-format UK Postcodes with the correct space position

Context

House
This article has been quite popular, so I have updated it with some more information and cleaned up the formatting, as a couple of spaces were missed out, so it can be copied straight into SSMS now.

It uses the following steps;

  • Remove everything from the text string (including spaces) apart from numbers and letters
  • Replaces the standard typing mistakes like;
  • Portsmouth is PO, Colchester is CO, but sometimes gets entered as P0 or C0 (a zero)
  • Ilford and Reading are IG and RG, but sometime get entered with a 6 in place of the G
  • Work out format the postcode should be in, and insert a space where it should be.
  • Check that the format is correct and return a new value if true, else return the old format.

Please note, this does not check whether the post code actually exists, and further reading can be found on the following link Postcodes in the United Kingdom.

SQL

CREATE FUNCTION CleansePostCode (@PostCode VARCHAR(100)) RETURNS VARCHAR(100) AS BEGINDECLARE @OldPostCode VARCHAR(100)SET @OldPostCode=LTRIM(RTRIM(@PostCode))
--Clean to Numbers and LettersDECLARE @Letter INTSET @Letter = PATINDEX('%[^0-Z]%',@PostCode)BEGINWHILE @Letter>0BEGINSET @PostCode = STUFF(@PostCode,@Letter,1,'')SET @Letter = PATINDEX('%[^0-Z]%',@PostCode)ENDEND
--Replace obvious errorsSET @PostCode =(CASE WHEN LEFT(@PostCode,2)='P0' THEN STUFF(@PostCode,1,2,'PO')WHEN LEFT(@PostCode,2)='C0' THEN STUFF(@PostCode,1,2,'CO')WHEN LEFT(@PostCode,2)='I6' THEN STUFF(@PostCode,1,2,'IG')WHEN LEFT(@PostCode,2)='HO' THEN STUFF(@PostCode,1,2,'HD')WHEN LEFT(@PostCode,2)='C8' THEN STUFF(@PostCode,1,2,'CB')WHEN LEFT(@PostCode,2)='D0' THEN STUFF(@PostCode,1,2,'DO')WHEN LEFT(@PostCode,2)='H5' THEN STUFF(@PostCode,1,2,'HS')WHEN LEFT(@PostCode,2)='0L' THEN STUFF(@PostCode,1,2,'OL')WHEN LEFT(@PostCode,2)='0X ' THEN STUFF(@PostCode,1,2,'OX')WHEN LEFT(@PostCode,2)='P0' THEN STUFF(@PostCode,1,2,'PO')WHEN LEFT(@PostCode,2)='R6' THEN STUFF(@PostCode,1,2,'RG')ELSE @PostCode END)
 --Works out correct pattern and insert spaceSET @PostCode = (CASE WHEN @PostCode LIKE '[A-Z][A-Z][0-9][0-9][A-Z][A-Z]' THEN LEFT(@PostCode,3)+' '+RIGHT(@PostCode,3)WHEN @PostCode LIKE '[A-Z][0-9][0-9][A-Z][A-Z]' THEN LEFT(@PostCode,2)+' '+RIGHT(@PostCode,3)WHEN @PostCode LIKE '[A-Z][0-9][0-9][0-9][A-Z][A-Z]' THEN LEFT(@PostCode,3)+' '+RIGHT(@PostCode,3)WHEN @PostCode LIKE '[A-Z][0-9][A-Z][0-9][A-Z][A-Z]' THEN LEFT(@PostCode,3)+' '+RIGHT(@PostCode,3)WHEN @PostCode LIKE '[A-Z][A-Z][0-9][0-9][0-9][A-Z][A-Z]' THEN LEFT(@PostCode,4)+' '+RIGHT(@PostCode,3)WHEN @PostCode LIKE '[A-Z][A-Z][0-9][A-Z][0-9][A-Z][A-Z]' THEN LEFT(@PostCode,4)+' '+RIGHT(@PostCode,3)ELSE @PostCode END)
--Update @NewPostCode to old value if pattern does not match, or else use new value.DECLARE @NewPostCode VARCHAR(100)IF (CASE WHEN @PostCode LIKE '[A-Z][A-Z][0-9] [0-9][A-Z][A-Z]' THEN 1WHEN @PostCode LIKE '[A-Z][0-9] [0-9][A-Z][A-Z]' THEN 2WHEN @PostCode LIKE '[A-Z][0-9][0-9] [0-9][A-Z][A-Z]' THEN 3WHEN @PostCode LIKE '[A-Z][0-9][A-Z] [0-9][A-Z][A-Z]'THEN 4WHEN @PostCode LIKE '[A-Z][A-Z][0-9][0-9] [0-9][A-Z][A-Z]' THEN 5WHEN @PostCode LIKE '[A-Z][A-Z][0-9][A-Z] [0-9][A-Z][A-Z]' THEN 6ELSE 0 END)=0BEGIN   SET @NewPostCode=@OldPostCodeENDELSESET @NewPostCode=@PostCodeRETURN @NewPostCodeEND

Author

Helpful?

Please note, this commenting system is still in final testing.
Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

Site Links

RSSLoginLink Cookie PolicySitemap

Social Media

facebook.com/Claytabaseinstagram.com/claytabase/twitter.com/Claytabaselinkedin.com/company/claytabase-ltd

Get in Touch

+15125961417info@claytabase.comClaytabase USA, 501 Congress Avenue, Suite 150, Austin, Texas, 78701, United States

Partnered With

The settings on this site are set to allow all cookies. These can be changed on our Cookie Policy & Settings page.
By continuing to use this site you agree to the use of cookies.
Ousia Logo
Logout
Ousia CMS Loader