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?
brianmistyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dqmqCommented:
>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.
0
brianmistyAuthor Commented:
>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.
0
dqmqCommented:
>I meant empty, and it certainly can be null or empty.

An integer datatype cannot be empty. It's either null or contains an integer.

>The question is, what do you do when the user does not have a state?

If you allow the FK to be null, then there is nothing else to do except use outer joins.  The join will not fail.  However, code that attemps to use null fields returned by an outer join may fail.

You can make the FK required and use a special value in the State table to represent no state. Personally, I think thats a pretty reasonable compromise.

Relational purists may suggest you add a UserState table to associate users with their states.  Users without states simply would not have a row in that table.

Another relational technique would be to split users with states and users without states into separate tables.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brianmistyAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.