1

Here is the code I tried

SELECT 
 case when InsertedRows is null then 0 else InsertedRows end InsertedRows
,case when FailedRows is null then 0 else FailedRows end FailedRows
,case when UpdatedRows is null then 0 else UpdatedRows end UpdatedRows
,InsertedRows + UpdatedRows + FailedRows as tot
FROM PATS.ImportLog
WHERE CreatedBy='suvaneeth' 
      AND ISNULL(CompletedYN,0) = 0 
      AND CAST(CreatedDate AS date) >= CAST(GETDATE() AS date)

and I get the result for tot is NULL

99  0   0   NULL

I'm expecting the result is 99

5
  • 2
    NULL + 0 = NULL Commented Jan 28, 2019 at 13:13
  • 1
    Possible duplicate of Addition with NULL values Commented Jan 28, 2019 at 13:14
  • there is no NULL values. All of them are converted into 0 Commented Jan 28, 2019 at 13:14
  • 1
    They are on another column expressions, not on your table. You would have to repeat the whole CASE again for that matter. Commented Jan 28, 2019 at 13:15
  • The tot column in the result just adds them directly, without regarding to nulls. There is at least one in either of them. Commented Jan 28, 2019 at 13:16

3 Answers 3

2
select 
case when InsertedRows is null then 0 else InsertedRows end InsertedRows
,case when FailedRows is null then 0 else FailedRows end FailedRows
,case when UpdatedRows is null then 0 else UpdatedRows end UpdatedRows
,COALESCE(InsertedRows,0) + COALESCE(UpdatedRows,0) + COALESCE(FailedRows,0) as tot
from PATS.ImportLog
WHERE CreatedBy='suvaneeth' AND ISNULL(CompletedYN,0)=0 AND CAST(CreatedDate AS date)>=CAST(GETDATE() AS date)
Sign up to request clarification or add additional context in comments.

Comments

1

You can try using ISNULL(insertedrows, 0) or COALESCE(insertedrows, 0) instead of CASE

Comments

0

Try in this way.

select 
  Coalesce(InsertedRows, 0) InsertedRows
  ,Coalesce(FailedRows, 0) FailedRows
  ,Coalesce(UpdatedRows, 0) UpdatedRows
  ,Coalesce(InsertedRows, 0) + Coalesce(FailedRows, 0)+ Coalesce(UpdatedRows, 0) as tot
from PATS.ImportLog
WHERE CreatedBy='suvaneeth' AND ISNULL(CompletedYN,0)=0 AND CAST(CreatedDate AS date)>=CAST(GETDATE() AS date)

2 Comments

There is no error message. It shows the same result what i was trying
are you sure that your where clause returns any result?

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.