how can i update a phone recored in sqlserver (tsql) ?


i have a table with correct and wrong phone numbers.

wrong phone number : i need to delete the third '0' and keep the rest of the number as it is

here there's no 0 before the 7, there's nothing to do.

what would be the query in order to repair this ?

thank you in advance.
Erwin PombettAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark WillsTopic AdvisorCommented:
Can we rely on length ?

because you could do

select left(phone,3) + right(phone,9)
where substring(phone,4,1) = '0'
and len(phone) = 13

Will have a better go at it in a couple of minutes...
Erwin PombettAuthor Commented:
Hello , yes, i think so ,
i'm goign to pass a query in order to check the lenght...

ok. there's spaces for somme of them, so i will remove teh spaces.
can  the following replace space with empty char ?

then for everbody i can remove the where ?

update person
set MobilePhone = replace(MobilePhone, ' ', '')
where personID = '90882'
Mark WillsTopic AdvisorCommented:
Might be safer to substring() rather than right() and definitely check before you start :)

select phone as OLD, len(phone) as [LEN],left(phone,3) + substring(phone,5,20) as  NEW
from yourtable 
where substring(phone,4,1) = '0'

Open in new window

Add in as much criteria as is revealed in the select before you start with the update. Test before you launch into updates... Take a copy / backup if it is a once off process.

Then (once happy) the update
Update yourtable set phone = left(phone,3) + substring(phone,5,20)
where substring(phone,4,1) = '0'
and len(phone) > 12

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark WillsTopic AdvisorCommented:
Yep, you can replace(phone,' ','') for all....

EDIT: gotta duck out for an hour....
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.