cast a null to integer (zero)


i need to make an update statement that does something like:

update table set col = value + (select sum(col) where type = 'sometype') where .....etc

but there is trouble when the sub query "select sum(col) where type = 'sometype'" returns null - the update fails because:

set col = value + ()

is not valid.

how can i make sure that "select sum(col) where type = 'sometype'" returns a zero instead of null?

Thanks and regards,  Mike.
LVL 37
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.

(select coalesce(sum(col), 0) where type = 'sometype')

But note, that coalesce is in general CASE clause. Look into it.

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
meverestAuthor Commented:
thanks for that tip - i will investigate and make sure that it is valid for my purpose...

will let you know shortly :)

earth man2Commented:
coalesce function takes the first non null argument as its result
update table set col = value + coalesce( ( select sum(col) from table_x where type = 'sometype' ),  0 ) where .....etc
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

meverestAuthor Commented:
beaut!  it works like a gem.

not sure what you mean by your note about CASE - my reading of the docs suggests that coalesce function is exactly for this sort of situation...

are you warning of some performance hit using these functions?

thanks again!

meverestAuthor Commented:
oh, thanks earthman2 as well - didn't notice your comment until i had responded to the earlier one.

I meant, that Coalesce is just CASE clause, but nicely called.
Details here:
meverestAuthor Commented:
OK, i think i get your meaning.  yet it still seems to me that even though coalesce is like a shorthand version of an equivalent case, it is a fairly specific case (no pun intended)

that is a CASE could be used in place of coalesce, but coalesce may not necessarily substiute a coalesce.

but nonetheless, now I have learned TWO new useful tricks with postgreSQL :) :)

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

From novice to tech pro — start learning today.