[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

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?
0
brianmisty
Asked:
brianmisty
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now