0

i'm planning to add sql values.

 Dim cmdStringc23 As String = " Update [Associate_wise_chart] 
        set [Hours]= (select SUM(tat) from [Dashboard].[dbo].[Dashboard] 
        where [assignee] like '%Santosh%') + (select SUM(tat) 
        from [Dashboard].[dbo].[requests] 
        where [assignee] like '%Santosh%') "

So consider, value from dashboard table is 10 value from requests table is NULL.

So I'm getting answer as 10+NULL = NULL.
I have set tat as NULL. My requirement , i have to display answer as 10 and not as NULL

Could any one have a look ?

4
  • 10 plus unknown is unknown! Commented Aug 10, 2016 at 11:50
  • Do something like set ColX = coalesce(nullable-select, ColX). Commented Aug 10, 2016 at 11:51
  • ok, can you help , how to define TAT column as null or not null. i should display the value irrespective of NULL. Commented Aug 10, 2016 at 11:53
  • If you set Tat column to NOT NULL you'll get an error instead... (A (sub)select with SUM returns NULL if no rows found.) Commented Aug 10, 2016 at 11:55

6 Answers 6

4

Use ISNULL function from SQL ...

Update [Associate_wise_chart] 
  set [Hours]= ISNULL((select SUM(tat) from [Dashboard].[dbo].[Dashboard] 
 where [assignee] like '%Santosh%'), 0) + ISNULL((select SUM(tat) 
from [Dashboard].[dbo].[requests]
 where [assignee] like '%Santosh%'), 0) "

That will get you ISNULL(10, 0) + ISNULL(NULL, 0) = 10 + 0 = 10

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

2 Comments

Actyally, ISNULL is not (ANSI) SQL, it's a product specific function.
Absolutely, and COALESCE() is the ANSI SQL function.
1

You can't.

NULL is a synonym for "unknown". Ten plus unknown is unknown. The unknown can be 0, -1, 42, or even 6E23+π/2. But it's still unknown.

This is why you get NULL.

Instead, check for NULLs, and only add the other value, if it is not NULL.

Comments

0

set NOTNULL while creating the table

1 Comment

Wont help. A (sub)select with SUM returns NULL if no rows found.
0

fast and easy just convert tat column into Not Null and have 0 in place of NULL.

Comments

0
Dim cmdStringc23 As String = " Update [Associate_wise_chart] 
    set [Hours]= (select isnull(sum(tat), 0) from [Dashboard].[dbo].[Dashboard] 
    where [assignee] like '%Santosh%') + (select isnull(sum(tat), 0) 
    from [Dashboard].[dbo].[requests] 
    where [assignee] like '%Santosh%') "

Use isnull

Comments

0

Update [Dashboard].[dbo].[Associate_wise_chart] set [Hours]= ISNULL ( (select SUM(tat) from [Dashboard].[dbo].[Dashboard] where [assignee] like '%Santosh%'),0) + ISNULL((select SUM(tat) from [Dashboard].[dbo].Unbuilds where [assignee] like '%Santosh%'),0) where [Name]='Santhosh'

ISNULL is working. Thank you Everyone.

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.