简体   繁体   English

Oracle使用NVL替换空检查空行

[英]Oracle replacing null using NVL check for empty rows

I am running a query to retrieve an integer but want to put a check for nulls. 我正在运行查询以检索整数,但要检查是否为空。 If null I want the query to return 0. How can I do that? 如果为null,我希望查询返回0。该怎么办? I tried this below but it's not working 我在下面尝试过此方法,但是它不起作用

select NVL(A_COUNT, 0) from MYTABLE where VEH_YEAR = '2003';

If A_COUNT is null I want the query to return 0. In the above case I don't have a value 2003 in VEH_YEAR column. 如果A_COUNT为null,我希望查询返回0。在以上情况下,我在VEH_YEAR列中没有值2003。 The query works if I have a value 2003 in VEH_YEAR column but A_COUNT is null. 如果我在VEH_YEAR列中具有值2003,但A_COUNT为空,则查询有效。

最好的版本是在使用NVL之前使用MAX类的Aggregate函数。

select NVL(MAX(A_COUNT),0) from MYTABLE where VEH_YEAR = '2003';
SELECT NVL((select A_COUNT from MYTABLE where VEH_YEAR = '2003'), 0) A_COUNT from dual;

这工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM