4

I have two columns ActivityCount and ParentValue. I created an expression:

ROUND((ActivityCount / ParentValue) / 100,16) * 100 

But the problem is it returns NULL for some columns and I wanted to replace NULL with 0. I can't seem to find the answers.

0

4 Answers 4

8

Expression:

ISNULL(ParentValue) || (ParentValue == 0) ? 0 : ROUND(ISNULL(ActivityCount) ? 0 : ActivityCount / ParentValue / 100,16) * 100

For readability:

ISNULL(ParentValue) || (ParentValue == 0) 
    ? 0 
    : ROUND((ISNULL(ActivityCount) 
                ? 0 
                : ActivityCount / ParentValue) / 100
             , 16) * 100

What it does:

  • If ParentValue field is NULL or has Zero, you do not have to perform a calculation because you will encounter Divide by Zero error. So, simply exit the expression by returning 0.

  • If ActivityCount field is NULL, replace it with zero before performing the calculation.

Recommendation:

I would recommend using COALESCE on the source query to replace the NULL values with zeroes and the calculation, if possible.

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

2 Comments

Again a top quality answer @Siva +1
Thank You Siva that was awesome and Thank you to everybody else much appreciated!!!
2

Test with ISNULL (SSIS, not the SQL expression), use the conditional operator

ISNULL(ROUND((ActivityCount / ParentValue) / 100,16) * 100) ? 0 : ROUND((ActivityCount / ParentValue) / 100,16) * 100

Comments

2

Try like below... it will help you...

ISNULL(ROUND((ActivityCount / ParentValue) / 100,16) * 100) || (ROUND((ActivityCount / ParentValue) / 100,16) * 100)= "" ? 0 : ROUND((ActivityCount / ParentValue) / 100,16

Comments

2

I would have said:

ISNULL(ActivityCount) || ISNULL(ParentValue) ? 0 : ROUND((ActivityCount/ParentValue)/100,16)*100

That way you are testing the simplest cases that will cause a NULL result rather than calculating the end case multiple times in your conditional.

(That being said, any of these will technically work.)

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.