开发者

Oracle: Show special text if field is null

I would like to write a select where I show the value of the field as normal except when the field is null. If it i开发者_StackOverflows null I'd like to show a special text, for example "Field is null". How would I best do this?

// Oracle newbie


I like to use function COALESCE for this purpose. It returns the first non-null value from given arguments (so you can test more than one field at a time).

SELECT COALESCE(NULL, 'Special text') FROM DUAL

So this would also work:

SELECT COALESCE(
   First_Nullable_Field, 
   Second_Nullable_Field, 
   Third_Nullable_Field, 
   'All fields are NULL'
) FROM YourTable


Just insert the NVL PL/SQL function into your query

SELECT NVL(SOMENULLABLEFIELD,'Field Is Null') SOMENULLABLEFIELD FROM MYTABLE;

More detail here : http://www.techonthenet.com/oracle/functions/nvl.php


You could also use DECODE:

select value, decode(value, NULL, 'SPECIAL', value) from 
  (select NULL value from dual
   union all
   select 2 value from dual
  )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜