Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

update with concat

Posted on 2006-07-10
8
Medium Priority
?
517 Views
Last Modified: 2008-02-01
Hello, I have a phone column with numbers such as 9999999999. I'm trying to update the data to reflect
(999) 999-9999 ext. and allow up to a four character extension in the future. The datatype of the column is varchar (25).

I'm trying to use the code below but get this error:
Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near '('.

Here is my SQL:
update Technician
set [phone] = concat "(" + left(3) + ")" + " " + right((left,6),3) + "-" + right((left, 10),4)" + " " + "Ext." " + " " + right((left, 12),4)
where phone is not null
0
Comment
Question by:gogetsome
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17075744
Here is my SQL:
update Technician
set [phone] = "(" + left(3) + ")" + " " + right((left,6),3) + "-" + right((left, 10),4)" + " " + "Ext." " + " " + right((left, 12),4)
where phone is not null
0
 

Author Comment

by:gogetsome
ID: 17075784
Thanks AngelIII, that was fast!
I'm getting this error: The left function requires 2 arguments.
0
 
LVL 41

Expert Comment

by:ralmada
ID: 17075887
you should use
left(yourfield, 3)

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 41

Expert Comment

by:ralmada
ID: 17075896
Like this
update Technician
set [phone] = "(" + left(yourfield, 3) + ")" + " " + right(left(yourfield,6),3) + "-" + right(left(yourfield, 10),4)" + " " + "Ext." " + " " + right(left(yourfield, 12),4)
where phone is not null
0
 
LVL 41

Expert Comment

by:ralmada
ID: 17075939
I didn't check it but I guess you have a few " more than the ones needed

Please try this

update Technician
set [phone] = "(" + left(yourfield, 3) + ")" + " " + right(left(yourfield,6),3) + "-" + right(left(yourfield, 10),4) + " " + "Ext. " + " " + right(left(yourfield, 12),4)
where phone is not null
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 17075954
I strongly recommend you  not store all the extra characters.  Sure, let the user type in them in they want, but, after verifying the format (of course), strip all non-numerics prior to storage.  This can be done at the client, to take load off SQL.

Also, store area code, phone number, and extension in separate columns.  Then use a computed column to SELECT a formatted column.

OK, now that I've got that off my chest, on to what you actually requested :-) :

UPDATE ...
SET [phone] = STUFF(STUFF(STUFF(phone, 1, 0, '('), 5, 0, ') '), 10, 0, '-')
WHERE phone NOT LIKE '(%'   --only format values not already formatted
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 17075964
Btw, use single quotes (') not double (") for delimiting literals; " depends on specific settings to work correctly, while ' does not.
0
 

Author Comment

by:gogetsome
ID: 17076071
Thanks Scott! That updated the column correctly. The other posts kept giving errors.
I will modify by table by adding an extension column and I can use a regular expression validator on the client side on the insert in the future. Thanks for the advice.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question