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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select and format zip code results

Author  Topic 

kking
Starting Member

3 Posts

Posted - 2008-05-19 : 12:13:47
In my select statement I concatenate city+state+zipcode. In some cases the zipcode is 9 digits but is missing the '-'. How can I format zipcode to be '#####-####' when > 5 characters from the select statment.
Thanks,
Ken

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-19 : 12:59:12
The formatting is always better to be done at your front end. If you still have to do at your front end,do like this:-

SELECT coalesce(city,'')+coalesce(state,'')+case when len(coalesce(zipcode,'')) > 5 then left(coalesce(zipcode,''),5) + '-' + right(coalesce(zipcode,''),4) else coalesce(zipcode,'') end from yourtable
Go to Top of Page
   

- Advertisement -