Query that returns 0 when no rows

Hi All, I have a query that selects the max id from a table but would like to try to ensure if no rows are available that it return 0 instead of null. I tried this query but it says it is missing a keyword:

select max(case hv_promas_protocol_id when 'null' then '0') as id from hv_protocol_master

Any ideas?
atxmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

miketobCommented:
Try with this:

select isnull((select max(MyID) from Table ),0) as MaxId

Open in new window

0
atxmanAuthor Commented:
Thanks for the quick response, I get an error 'ORA-00904: "ISNULL": invalid identifier'
0
sdstuberCommented:
SELECT NVL(MAX(hv_promas_protocol_id), 0) AS id FROM hv_protocol_master;

or

SELECT COALESCE(MAX(hv_promas_protocol_id), 0) AS id FROM hv_protocol_master;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

slightwv (䄆 Netminder) Commented:
Oracle is NVL:

NVL(value,0)
0
miketobCommented:
Oh Sorry, I thought it was for MSSQL, I'm not very familiar with oracle but try this as slightwv suggests:
select NVL((select max(MyID) from Table ),0) as MaxId

Open in new window

0
slightwv (䄆 Netminder) Commented:
>>I'm not very familiar with oracle but try this as slightwv suggests:

This isn't Oracle syntax and sdstuber already posted two options that should work.

Oracle needs to select FROM something.  The outer select needs a "from dual" if it was to be used.
0
sdstuberCommented:
no,  that's not correct syntax either  for oracle

I already posted 2 working examples for oracle syntax above.
0
atxmanAuthor Commented:
Thanks this is exactly what i needed
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.