How do I use COALESCE to return 'N/A' when the value of a TIMESTAMP column is NULL?

Vin*_*yak 0 postgresql coalesce sql-timestamp sql-null

Is it possible to use COALESCE (or any other way) to replace NULL values from a TIMESTAMP column with a string like 'N/A'?

In my SELECT statement I have a

CASE WHEN n.expiration_date::date IS NULL THEN 'N/A' ELSE n.expiration_date::date END
Run Code Online (Sandbox Code Playgroud)

When I try this, I get this error, which makes sense:

invalid input syntax for type date: "N/A"

I found this blog post too about this problem. Is there a way around it?

a_h*_*ame 5

CASE表达式的所有值都必须求值为相同的数据类型。

如果您确实需要N / A,则需要将日期转换为字符类型:

CASE 
    WHEN n.expiration_date IS NULL THEN 'N/A' 
    ELSE n.expiration_date::date::text 
END
Run Code Online (Sandbox Code Playgroud)

或者,如果您想控制格式:

CASE 
    WHEN n.expiration_date IS NULL THEN 'N/A' 
    ELSE to_char(n.expiration_date, 'YYYY-MM-DD')
END
Run Code Online (Sandbox Code Playgroud)