0

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? 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. The query works if I have a value 2003 in VEH_YEAR column but A_COUNT is null.

yalkris
  • 2,596
  • 5
  • 31
  • 51
  • SELECT NVL((select A_COUNT from MYTABLE where VEH_YEAR = '2003'), 0) A_COUNT from dual; This worked – yalkris Apr 30 '15 at 23:13

2 Answers2

3

Better version of your uery would be, using an Aggregate function like MAX before using NVL.

select NVL(MAX(A_COUNT),0) from MYTABLE where VEH_YEAR = '2003';
Maheswaran Ravisankar
  • 17,652
  • 6
  • 47
  • 69
0
SELECT NVL((select A_COUNT from MYTABLE where VEH_YEAR = '2003'), 0) A_COUNT from dual;

This worked.

yalkris
  • 2,596
  • 5
  • 31
  • 51