Oracle:如果字段为空则显示特殊文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5511028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:12:54  来源:igfitidea点击:

Oracle: Show special text if field is null

oracleselectnull

提问by Svish

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 is null I'd like to show a special text, for example "Field is null". How would I best do this?

我想写一个选择,在那里我正常显示字段的值,除非该字段为空。如果它为空,我想显示一个特殊的文本,例如“字段为空”。我怎么做最好?

// Oracle newbie

回答by Tommi

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

我喜欢COALESCE为此目的使用函数。它从给定的参数返回第一个非空值(因此您可以一次测试多个字段)。

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

回答by Pepto

Just insert the NVL PL/SQL function into your query

只需将 NVL PL/SQL 函数插入您的查询中

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

从 MYTABLE 中选择 NVL(SOMENULLABLEFIELD,'Field Is Null') SOMENULLABLEFIELD;

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

更多细节在这里:http: //www.techonthenet.com/oracle/functions/nvl.php

回答by Frank Schmitt

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
  )