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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Change existing text to phone format

Author  Topic 

reynet
Starting Member

4 Posts

Posted - 2007-04-27 : 14:02:23
I have a text field that stores phone numbers in the following format
5555555555
I need change the existing text to this format
(555)555-5555

I basically want to change the filed data if the length is 10 characters that way I will only change data that is in the wrong format. Anyway to do this easily with a simply update query? Any help would be most appreciated.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-04-27 : 14:16:38
This is a display issue. You should be storing the data as 10 digits, then your application should format it when viewing the data.

Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

reynet
Starting Member

4 Posts

Posted - 2007-04-27 : 14:20:05
That's the problem the application stores it in the phone format and is expecting it this way. The user imported all the numbers from a spread sheet that had the straight number format and the application screws up the number when displayed.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-04-27 : 14:24:44
Check this out:
DECLARE @s char(10)
SET @s = '1234567890'
SELECT '(' + SUBSTRING(@s, 1, 3) + ')' + SUBSTRING(@s, 4, 3) + '-' + SUBSTRING(@s, 7, 4)

You can use it in an update statement. Just substitute your column for my variable.


Tara Kizer
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

reynet
Starting Member

4 Posts

Posted - 2007-04-27 : 14:38:44
Thanks
Worked great!
Go to Top of Page
   

- Advertisement -