I have a telephones database table with 4 columns:
countrycodeareacodephonenumberextension
I’m following the “Canonical” formatting, since I have some international numbers to consider.
And as fate would have it, some numbers entered have extensions.
I have a query that I use to auto-format everything, except the extension:
SELECT CONCAT('+',`countrycode`,'(',`areacode`,')', `phonenumber`) AS `fullnumber` ...
result: +1(123)4567890 or: +61(08)1234 1234
This works great, and it works perfectly with some dialer software I export to.
However, I now need to include the extension in this query. However, I need to auto-format it like so:
+1(123)4567890 x123
That’s rather easy, but since most numbers do not have an extension, I need to make a condition that will include the char (x). And this is my dilemma, since I’m still learning conditional statements in a query.
Anybody work with weird if/else statements inside a CONCAT?
MySQL’s
IFstatement works like this:IF(condition, iftrue, iffalse): If the given condition is true, return theiftrueparameter. Otherwise, return theiffalseparameter.So, your query could look something like this:
The MySQL
IFfunction is one of several control flow functions. For more information on those functions, see the MySQL docs.