开发者

Oracle: Show special text if field is null

开发者 https://www.devze.com 2023-02-21 00:58 出处:网络
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 n

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

精彩评论

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