Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Como faço para exibir texto em um campo vazio em uma consulta sql?


Você pode usar um CASE :
select concat(first_name, " ", last_name) as CustomerName, 
   address, 
   case when address2 is null then 'None Provided' else address2 end as address2, 
   postal_code 
from customer
inner join address 
using (address_id);

Ou ainda melhor COALESCE :
select concat(first_name, " ", last_name) as CustomerName, 
   address, 
   COALESCE(address2,'None Provided') as address2, 
   postal_code 
from customer
inner join address 
using (address_id);

Se os dados contiverem uma string vazia ou nula, eu consideraria usar:
select concat(first_name, " ", last_name) as CustomerName, 
   address, 
   case 
     when address2 is null or address2 = '' 
     then 'None Provided' 
     else address2 end as address2, 
   postal_code 
from customer
inner join address 
using (address_id);