Link to home
Start Free TrialLog in
Avatar of brianmisty
brianmisty

asked on

How to join 2 tables when one table has empty or null values

I am trying to join 2 tables.. a User table and a States table.  They are related via "stateID" and "fkStateID" and the field is an integer.

Recently, someone from another country joined and since they do not have a state, the state field is blank (not NULL).  This is causing joins (including outer joins) to fail  with the error ... "Error converting data type varchar to numeric."

I have considered adding a none option to the states table, and then using that to populate the User table with a value, but that seemed a little messy.  Is there another, better alternative?
Avatar of dqmq
dqmq
Flag of United States of America image

>They are related via "stateID" and "fkStateID" and the field is an integer.

>since they do not have a state, the state field is blank (not NULL).

Those two statements are confusing me.  An integer field cannot be blank.

>This is causing joins (including outer joins) to fail  with the error ... "Error converting data type varchar to numeric."

Please don't tell me you are trying to join an integer field with a varchar field.
Avatar of brianmisty
brianmisty

ASKER

>Please don't tell me you are trying to join an integer field with a varchar field.

They are both integers. It is a standard state lookup table.  The question is, what do you do when the user does not have a state?

> An integer field cannot be blank.

I meant empty, and it certainly can be null or empty.  Whether it should or not is the question.
ASKER CERTIFIED SOLUTION
Avatar of dqmq
dqmq
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You were completely right.  I had failed to convert the field to an "int", even though I swear that remember doing it. Now all the blanks are converted to 0, which makes perfect sense, of course.  I also appreciate your summary of the state relationship issue.