如何在SQL查询结果中将NULL作为特定字符串返回?

时间:2015-09-28 23:16:53

标签: sql oracle null coalesce

我在查询时尝试将NULL作为字符串返回到视图中,但并不是真的了解如何。

对于列,它要么有数据要么返回NULL,但是如果它返回NULL我想要它说些别的话。

SELECT lib_item_id "Library Item ID",
       title "Library Item Name", 
       date_of_purchase "Year of Purchase",
       coalesce(pub_id, null) as "Publisher ID"
  FROM lib.Library_items 
 WHERE date_of_purchase > '31-DEC-10'
 ORDER BY "Library Item Name" asc;

1 个答案:

答案 0 :(得分:3)

使用

select coalesce(some_column, 'some default string') as some_column
from your_table

select case when some_column is null
            then 'some default string'
            else some_column 
       end as some_column
from your_table