Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Null problem in Update Query

Author  Topic 

Rmadler
Starting Member

3 Posts

Posted - 2010-02-06 : 14:01:41
Hello, the following query works fine as long as all joins return rows.
In the event a join does not return a row, SubJob.JobCost is updated with a Null. I need to not update the JobCost or add 0 to the jobCost. Please help.

Update T1
set T1.JobCost = T1.JobCost +
(
Select Sum(T3.SubContratorTotal)
FROM Route T2
INNER JOIN SubcontractorTime T3
ON T2.RouteNbr = T3.RouteNbr
and T2.RouteStatusId <> 3
WHERE T2.JobSubNbr = T1.JobSubNbr
)
From SubJob T1

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-02-06 : 14:15:13
Try ISNULL(Sum(T3.SubContratorTotal),0)


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-07 : 10:49:27
quote:
Originally posted by webfred

Try ISNULL(Sum(T3.SubContratorTotal),0)


No, you're never too old to Yak'n'Roll if you're too young to die.


will that work? In event of no matches from inner query will isnull() help when put with sum().
I think it must be

Update T1
set T1.JobCost = T1.JobCost +
ISNULL(
(
Select Sum(T3.SubContratorTotal)
FROM Route T2
INNER JOIN SubcontractorTime T3
ON T2.RouteNbr = T3.RouteNbr
and T2.RouteStatusId <> 3
WHERE T2.JobSubNbr = T1.JobSubNbr
),0)
From SubJob T1
Go to Top of Page
   

- Advertisement -