[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 473
  • Last Modified:

SQL Modify phone numbers in fields

Hi there,

I'm looking for help with the SQL code to modify a phone numbers in a column. I'm not sure what to use.

Right now they are like this:

234-555-2323
234-555-1234
234-555-2341

And I would like them to be like this:

(234) 555-2323
(234) 555-1234
(234) 555-2341

Thanks!
Primus
0
PrimusPilus
Asked:
PrimusPilus
2 Solutions
 
Aneesh RetnakaranDatabase AdministratorCommented:
declare @str varchar(100 ) = '250-516-4499'
select '('+STUFF(@str,4,1,')' )
0
 
Scott PletcherSenior DBACommented:
I'd suggest adding a WHERE condition so you can re-run the statement if needed but still not re-do a number already reformatted.

UPDATE dbo.tablename
SET
    phone_number =  '(' + LEFT(phone_number, 3) + ') ' + SUBSTRING(phone_number, 5, 100)
WHERE
    --_ is any char; [-] is exactly a -; % is match anything
    phone_number LIKE '___[-]___[-]____%'
0
 
PrimusPilusAuthor Commented:
Thank you guys!
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Tackle projects and never again get stuck behind a technical roadblock.
Join Now