Solved

update with concat

Posted on 2006-07-10
8
511 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 142

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 69

Accepted Solution

by:
Scott Pletcher earned 500 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 69

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now