0

I have a query which add many thing in a expression. Like this select a+b+c+d+e ....
But some of them may be null and I want to treat null value as 0.

I know I can use IFNULL like select ifnull(a,0)+ifnull(b,0)+ifnull(c,0)+ifnull(d,0)+ifnull(e,0), but I have many queries and this seems laboursome.

Is there some easy way like a function to achieve the same result?

5
  • Another option is the COALESCE function. Commented Nov 18, 2020 at 4:04
  • But that's basically the same as ifnull Commented Nov 18, 2020 at 6:23
  • 1
    Please tag the actual RDBMS you are using. I'm guessing mysql, as sql server doesn't have an IFNULL function afaik. Commented Nov 18, 2020 at 6:40
  • No, there is no easier way. Commented Nov 18, 2020 at 7:23
  • No, there isn't a shortcut. IFNULL if you want to handle NULLs... Commented Nov 18, 2020 at 17:43

1 Answer 1

0

As mentioned in the comments, you will need to use indeed either IFNULL or IF.

However, if you have lots of them, and you are in control of the schema, I would suggest defining your fields as .. NOT NULL DEFAULT 0 (explicit default is good). This way, you will not need IFNULL any longer.

Sign up to request clarification or add additional context in comments.

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.