sql >> Database teknologi >  >> RDS >> Mysql

Hvordan viser jeg tekst i et tomt felt i en sql-forespørgsel?

Du kan bruge en 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);

Eller endnu bedre 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);

Hvis dataene indeholder en tom streng eller null, vil jeg overveje at bruge:

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);


  1. Oracle SQL - Generer og opdater en kolonne med forskellige tilfældige tal i hver række

  2. Installation af Postgres på vinduer til brug med Ruby-on-Rails

  3. MySQL ABS() Funktion – Returner den absolutte værdi af et tal

  4. hvordan man henter dataene i JSP inde i tabel baseret på drop down valg