Solved

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

Posted on 2014-04-02
9
273 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
  • 4
  • 3
  • 2
9 Comments
 
LVL 31

Expert Comment

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

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
 
LVL 34

Expert Comment

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

WHERE SUBSTRING(field,1,1) <> '+'
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 34

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 34

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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.​
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

18 Experts available now in Live!

Get 1:1 Help Now