Solved

update with concat

Posted on 2006-07-10
8
509 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
 
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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:
ScottPletcher 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:ScottPletcher
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

760 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