Reputation: 3227
I have a SQL Server table with one column that stores Canadian Postal Codes. Sometimes they're in the proper format H0H 0H0, and othertimes they're written like H0H0H0 or H0H-0H0. I'm looking to be able to update all records to standardize the format as H0H 0H0 but I cannot figure out the SQL. Any help would be appreciated.
Upvotes: 0
Views: 7110
Reputation: 74307
Assuming that the following constraints hold:
This should do the trick, for any number of characters in the field.:
update PostalCodes
set PostalCode = stuff( PostalCode , 4 , len(PostalCode) - 6 , ' ' )
Alternatively, you could do something like this — a case statement to look for all the different ways people have found to create junk postal codes and treat each according to its needs:
update PostalCodes
set PostalCode = case
when PostalCode like '[A-Z][0-9][A-Z][ -][0-9][A-Z][0-9]' then ...
when PostalCode like '![A-Z][0-9][A-Z][0-9][A-Z][0-9]' then ...
...
else PostalCode
end
A select statement like
select PostalCode,count(*) from PostalCodes where PostalCode not like '[A-Z][0-9][A-Z] [0-9][A-Z][0-9]' group by PostalCode order by 2 desc
should show you the junk data.
Upvotes: 0
Reputation: 3972
UPDATE PostalCodes SET PostalCode = LEFT(PostalCode, 3) + ' ' + RIGHT(PostalCode, 3 )
This assumes that the postal code field has already been trimmed on both the left and right sides. It literally grabs the left 3 digits, places a space and then the right 3 digits.
Upvotes: 5