Solved

Add +39 to beginning of mysql field only if field not beginning with +

Posted on 2014-04-02
9
281 Views
Last Modified: 2014-04-02
Hi all...

I have a lot of contacts in my database but some of them haven't international prefix.

I'd need an update query able to see if the number begins with "+" character.
If so, skip it, otherwise update its field preponing "+39".

Thanks for the help!
0
Comment
Question by:ltpitt
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 39972677
update table
set field = '+39' + field
where substring(field,1,1) <> '+';
0
 
LVL 32

Expert Comment

by:awking00
ID: 39972690
Sorry, didn't see MySQL -
update table
set field = concat('+39',field)
where substring(field,1,1) <> 0
0
 
LVL 1

Author Comment

by:ltpitt
ID: 39972755
Excuse me if I forgot to mention this...

Will this query skip empty fields?

I'd like it to do so...

Just now I was thinking that if it doesn't find the + it could also mean that the field is empy.

Sorry for being a little slow :)
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39972757
@awking00: you forgot the +

WHERE SUBSTRING(field,1,1) <> '+'
0
 
LVL 1

Author Comment

by:ltpitt
ID: 39972810
I tried:

update contatti
set cellulare = concat('+39',cellulare)
WHERE SUBSTRING(cellulare,1,1) <> '+'

but it addedd +39 to empty fields
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39972822
Of course. We only tested if it has a + at the beginning.

To test for empty fields use
UPDATE contatti
SET cellulare = CONCAT('+39',cellulare)
WHERE SUBSTRING(cellulare,1,1) <> '+' AND cellulare <>''
0
 
LVL 1

Author Closing Comment

by:ltpitt
ID: 39972825
Hail to the sql king :)
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 39972828
Glad I could help, but this is awking00's solution.

I just improved it slightly. Please re-grade accordingly.

Thank you.
0
 
LVL 1

Author Comment

by:ltpitt
ID: 39972884
Oh sorry I thought you were the same user...

I'll try to fix this...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

735 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