We help IT Professionals succeed at work.
Get Started

Mysql SELECT: possible to concantenate fields ONLY if they contain data?

freestate asked
Last Modified: 2012-05-09
I have a database driven eCommerce site, that captures address info in three fields:


However, my fulfilment provider only accepts two fields:


I have been able to CONCAT Address2 and Address3, placing ', ' in between them - however, if there is no Address2 or no Address3 this results in a pointless comma, which breaks the fulfilment system!

Is it possible to somehow check if Address2 and Address3 contain data, and only then CONCAT with a comma in between them?

Here's the query in use at the moment:

SELECT o.id AS orderID, o.shiptotal AS fPostageCost, CONCAT(o.dateyear,'-',o.datemonth,'-',o.dateday) AS dReceievedDate, o.ordertotal AS fTotalCharge, o.taxctotal AS fTax, 'GBP' AS cCurrency, '' AS PostalServiceTag, o.id AS ReferenceNum, CONCAT(oi.fname,' ',oi.lname) AS cFullName, o.customtwo AS cShippingAddress, oi.postalcode AS cPostCode, oi.country AS Country, o.email AS cEmailAddress, '' AS PackagingGroup, o.customone AS addCompany, CONCAT(oi.fname,' ',oi.lname) AS addName, oi.addone AS addAddress1, CONCAT(oi.addtwo,', ',oi.addthree) AS addAddress2, oi.city AS addTown, oi.stateprov AS addCounty, oi.country AS addCountry, oi.postalcode AS addPostCode, o.phone AS CustomerPhoneNumber
FROM ecom_orders o, ecom_orderitems oi WHERE status = 'NS' AND o.id = oi.orderid GROUP BY o.id

Open in new window

Watch Question
This problem has been solved!
Unlock 2 Answers and 12 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE