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.

1
  • SELECT NVL((select A_COUNT from MYTABLE where VEH_YEAR = '2003'), 0) A_COUNT from dual; This worked Commented Apr 30, 2015 at 23:13

2 Answers 2

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';
Sign up to request clarification or add additional context in comments.

Comments

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

This worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.