I Have the following SQL Case statement which works perfectly as long as the Years match MF.Date & M.MemberCurrentYear. There will be scenarios where the years do not match and this returns a Balance of NULL. I would like it to return a Zero.
SELECT SUM(CASE WHEN Type = 1 THEN Amount ELSE Amount * - 1 END) AS Balance
FROM dbo.MemberFinancials AS MF
INNER JOIN dbo.Members AS M ON MF.MemberID = M.MemberID
AND DATEPART(yyyy, MF.Date) = M.MemberCurrentYear
INNER JOIN dbo.FinancialTypes AS FT ON MF.FinancialTypeID = FT.FinancialTypeID
Thanks
SELECT
ISNULL(SUM(CASE WHEN Type = 1 THEN Amount ELSE Amount * - 1 END),0) AS Balance
FROM dbo.MemberFinancials AS MF INNER JOIN
dbo.Members AS M ON MF.MemberID = M.MemberID AND DATEPART(yyyy, MF.Date) = M.MemberCurrentYear INNER JOIN
dbo.FinancialTypes AS FT ON MF.FinancialTypeID = FT.FinancialTypeID
Use ISNULL (Or COALESCE if you prefer...)
SELECT ISNULL( SUM(CASE WHEN Type = 1 THEN Amount ELSE Amount * - 1 END), 0) AS Balance
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With