'Postgresql - Return empty string on left join instead of null

I have two tables :

Person table

idPerson (uuid) name (varchar) creationDate(date)
1 bob 2022-01-01
2 sam 2022-01-02

Telephone table

idTelephone (uuid) number(varchar) actif(boolean) valid(boolean) idPerson (uuid) creationDate(date)
1 34343443 true true 1 2022-02-22
1 35676878 true false 1 2022-02-15
2 32432 false false 2 2022-02-24

SQL :

select p.idPerson, t.number
from person p
left join telephone t on t.idPerson = p.idPerson and t.actif=true and t.valid =true

where t.creationDate >'2022-02-22';

The above query will return only the first row for personId = 1.

Question : I want the query to return empty String for personId = 2. How can I achieve this please?

Please note that , a person can have multiple telephone numbers but only 1 valid and the remaining will be valid = false.

Current Result:

1     34343443

Expected Results:

1     34343443
2     ' '


Solution 1:[1]

Your query will return a row for idperson number 2, but the number will be NULL.

To get an empty string instead, use

coalesce(t.number, '')

Solution 2:[2]

you can use case when

   select p.idPerson, 
   case when t.number is null then ''' ''' else
   t.number end as nm
    from person p
    left join telephone t on t.idPerson = p.idPerson
     and t.actif=true and t.valid =true

demo link

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Laurenz Albe
Solution 2