SQL-Statement. Convert 'NULL' to 0

i have the following update:

UPDATE test2
SET test2.id= (select max(test1.id) from test1 where  test1.bez = test2.bez);

now the select '(select max(test1.id) from test1 where  test1.bez = test2.bez);' can return 'NULL'. But in table test2.id NULL is not allowed. So i must convert 'NULL' to 0.

like

UPDATE test2
SET test2.id= (select ConvertNULL(max(test1.id)) from test1 where  test1.bez = test2.bez);


or the select should not update 'NULL' values but i dont want a select like because i think its to slow:

UPDATE test2
SET test2.id= (select max(test1.id) from test1 where  test1.bez = test2.bez)
where (select max(test1.id) from test1 where  test1.bez = test2.bez) is not NULL;

timemasterxAsked:
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.

rramineniCommented:
How about using nvl function instead of complicating the statements.


UPDATE test2
SET test2.id= (select nvl(max(test1.id),0) from test1 where  test1.bez = test2.bez);

or

UPDATE test2
SET test2.id= (select max(nvl(test1.id,0)) from test1 where  test1.bez = test2.bez);
0
andrewstCommented:
To avoid updating the rows where the MAX is NULL:

UPDATE test2
SET test2.id= (select max(test1.id) from test1 where  test1.bez = test2.bez)
where exists (select 1 from test1 where test1.bez = test2.bez and test1.id is not NULL);

Or to set the ID to 0:

UPDATE test2
SET test2.id= (select NVL(max(test1.id),0) from test1 where  test1.bez = test2.bez);



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
timemasterxAuthor Commented:
thanks, all solutions worked but i use this one

UPDATE test2
SET test2.id= (select max(test1.id) from test1 where  test1.bez = test2.bez)
where exists (select 1 from test1 where test1.bez = test2.bez and test1.id is not NULL);

becase it works under MS SQL-Server too.

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.