数字数据类型的 NVL 的 Oracle 等价物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25914062/
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
Oracle equivalent of NVL for number datatype
提问by b00kgrrl
What is the Oracle equivalent of NVL for number datatypes?
数字数据类型的 NVL 的 Oracle 等价物是什么?
This works since the datatype is VARCHAR:
这是有效的,因为数据类型是 VARCHAR:
select nvl(enrol.OUAC_APPLNO, 'blank') Invalid_OUAC_APPLNO
But this doesn't work since the datatype is NUMBER:
但这不起作用,因为数据类型是 NUMBER:
select nvl(enrol.ouac_type_id, 'blank') REGTYP
回答by Ben
There is no equivalent and no Oracle functions will accept this (apart from DECODE()
but don't do that); you're going to have to convert your number to a character:
没有等价物,也没有 Oracle 函数会接受这个(除了DECODE()
但不要这样做);你将不得不将你的数字转换为一个字符:
select nvl(cast(enrol.OUAC_APPLNO as varchar2(10)), 'blank')
You may need to change the number of characters you're converting to as appropriate.
您可能需要根据需要更改要转换为的字符数。
However, I don't know why you're doing this at all. By definition a NULL implies non-existence. If you want to display blank
in order to confer non-existence this is something that you should be doing with your presentation layer rather than the database.
但是,我完全不知道您为什么要这样做。根据定义,NULL 意味着不存在。如果你想显示blank
以赋予不存在,这是你应该用你的表示层而不是数据库来做的事情。
回答by dursun
it wont work because oracle wants every row should be in the same type otherwise you cannot run functions on that column, you have cast ouac_type_id to be varchar as below;
它不会工作,因为 oracle 希望每一行都应该是相同的类型,否则你不能在该列上运行函数,你已经将 ouac_type_id 强制转换为 varchar,如下所示;
select nvl(cast(enrol.OUAC_APPLNO as varchar2(10)), 'blank') REGTYP
回答by Pradeep Bhandarkar
select nvl(TO_CHAR(enrol.OUAC_APPLNO), 'blank') REGTYP
选择 nvl(TO_CHAR(enrol.OUAC_APPLNO), 'blank') REGTYP