Solved

update with concat

Posted on 2006-07-10
8
513 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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

733 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