1

I have 2 temp tables that I am populating with a numeric value:

CREATE TABLE #Initial
(
[SalesTax] NUMERIC (20,4),
[Unit] VARCHAR(10)
);

INSERT INTO #Initial ([SalesTax],[Unit])
(
SELECT SUM(SalesTax), Unit
FROM Billing
GROUP BY Unit
);

CREATE TABLE #End
(
[SalesTax] NUMERIC (20,4),
[Unit] VARCHAR(10)
);

INSERT INTO #End([SalesTax],[Unit])
(
SELECT SUM(SalesTax), Unit
FROM BillingEnd
GROUP BY Unit
);

Then I am trying to add these values together when I select them in another query(This query is a very simple example of a much bigger query. It has to be done like this):

SELECT 
u.Unit,
i.SalesTax,
e.SalesTax,
(i.SalesTax+ e.SalesTax) AS SalesTaxFeeTotal
FROM tblUnit u
LEFT JOIN #Initial i ON u.Unit = i.Unit
LEFT JOIN #End e ON u.Unit = e.Unit

For some reason the SalesTax column is coming back as NULL when the i.SalesTax and e.SalesTax column are returning values for those same records.

There is no NULL values in either temp table. If there is no fee, it is 0.00

Why are these retuning nulls?

2
  • Your not adding StateFeeBegin and StateFeeEnd but StateFee instead. Commented Jul 21, 2011 at 17:01
  • 1
    Can you edit your question? Columns StateFeeBegin and StateFeeEnd are not defined in #Initial #End declarations. Also, the last query is missing commas after i.StateFeeBegin and e.StateFeeEnd Commented Jul 21, 2011 at 17:06

2 Answers 2

5

Change this

(i.StateFee + e.StateFee) AS StateFeeTotal

to

(ISNULL(i.StateFee,0) + ISNULL(e.StateFee,0)) AS StateFeeTotal

Or with maybe correct column name

(ISNULL(i.StateFeeBegin,0) + ISNULL(e.StateFeeEnd,0)) AS StateFeeTotal

With the LEFT JOIN, you can get NULLs for the values. NULL added to anything else will always be NULL. This answer uses ISNULL to replace NULL vales.

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

Comments

1

You select StateFeeBegin and StateFeeEnd but you add two columns called StateFee from Initial and End. Use ISNULL(YourField, 0) to replace any null occurrence with 0.

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.