1

I have doubt on my SQL.

Please find the query

select a.id, a.phone, b.list,c.ctitle,l,
       if(last_id!=0 and date(activity)=date(start_date), 
       CONCAT(ag.firstname,' ‘,ag.lastname) ,’')
from table name a 
left join <all other tables>
where a.cid = '100' and a.date between '2013-05-01' and '2015-06-14';

The output is showing properly.currently i am getting firstname and lastname. But for some entries first name and last name empty. I need to modify the query so that i can print the non-null value of the column if(last_id!=0 and date(activity)=date(start_date), CONCAT(ag.firstname,' ‘,ag.lastname) ,’') ie same query excluding the null entries for firstname and last name. I tried IS NOT NULL but not working.

Please help

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Sweety
  • 77
  • 8

2 Answers2

4

If I understand correctly, you have a problem with NULLs. One simple solution is to use concat_ws():

   concat_ws(' ', ag.firstname, ag.lastname)

If either name is NULL, then it is just ignored in the concatenation.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

I guess you can find the answer here :MySQL CONCAT returns NULL if any field contain NULL

also you can use IFNull syntax for this:

CONCAT(IFNull(ag.firstname,''),IFNull(ag.lastname,''))