0

Rather than have a NULL in a column, I want a 0 to be present.

Given the following two tables:

TABLE1
ClientID        OrderCount
1               NULL
2               NULL
3               NULL
4               NULL


Table2
ClientID        OrderCount
1               2
3               4
4               6

NOTE: The OrderCount column in both tables is INT datatype.

UPDATE TABLE1
SET OrderCount = ISNULL(TABLE2.OrderCount,0)
FROM TABLE1
INNER JOIN TABLE2 ON TABLE2.ClientID = TABLE1.CLIENTID

When I look at table1, I see this:

ClientID        OrderCount
1               2
2               NULL
3               4
4               6

So, I thought to myself - "Obviously, I should be using NULLIF and not ISNULL", so I reversed them. Same result.

What am I doing wrong here? How do I get a 0 rather than a NULL in the column?

3 Answers 3

6

You need a LEFT JOIN rather than an INNER JOIN. The records that don't have a matching ClientID are not even being touched by your query.

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

Comments

2

you are using INNER JOIN but you don't have client ID 2 on table2 so your result set wont include a line with 2. Replace it with LEFT JOIN

Comments

0

Your join is probably filtering out rows.

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.