Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
deanglen
Yak Posting Veteran
65 Posts |
Posted - 2013-03-20 : 08:28:52
|
HiBeen searching for a solution to a problem we have but so far no luck.We have a Postcode column in our customer.dbo SQL table. This is for the Post/Zip code of their address.Our couriers are wanting the Postcode to be in a certain format. ExampleBS225DU in the field needs to be BS22 5DUThe problem is that UK postcodes all have different lengths..exampleW1 5DUBS22 4LXC22 3GTOne common factor is that they all end in 3 digits that are Numeric, alpha, alphaIs there any SQL script that someone knows of that can convert the postcodes to upper case and add a space fourth character to the left from the end so that the postcodes are split correctly?Any suggestions would be welcome! |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2013-03-20 : 09:16:10
|
-- testtable and testdatadeclare @sample table(postcode varchar(255))insert @sampleselect 'W15DU' union allselect 'Bs224LX' union allselect 'C223gT'-- solutionselectpostcode as orig,upper(reverse(stuff(reverse(postcode),4,0,' '))) as formattedfrom @sample--edit: added UPPER() Too old to Rock'n'Roll too young to die. |
|
|
deanglen
Yak Posting Veteran
65 Posts |
Posted - 2013-03-20 : 10:45:21
|
Thanks I can see the logic worked fine. So to replace the column values Zip in Address.dbo what would be the script to run? |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2013-03-20 : 10:54:51
|
I would not replace the values in the table. I would do the formatting on the fly when selecting the data.But if you want to change the data in your table then (have a backup) do this:update YourTableset PostcodeColumn = upper(reverse(stuff(reverse(PostcodeColumn),4,0,' '))) Maybe you can add a where clause to change only UK postcodes? Too old to Rock'n'Roll too young to die. |
|
|
|
|
|