Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3492152
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:43:26+00:00 2026-05-18T11:43:26+00:00

I am doing a join between two tables and want to select the columns

  • 0

I am doing a join between two tables and want to select the columns based on whether they have a record or not. I’m trying to avoid having multiple of the same field and am trying to condense them into single columns. Something like:

Select 
    id = (CASE WHEN a.id IS NULL THEN b.id ELSE a.id END),
    name = (CASE WHEN a.name IS NULL THEN b.name ELSE a.name END)
From Table1 a
Left Join Table2 b
On a.id = b.id
Where a.id = @id

I’d like id to populate from Table1 if a record exists, but if not pull from Table2. The previous code returns no records because there are no NULL values in Table1 so my question is how do I run a check to see if any records even exist? Also if anyone knows of a better way to accomplish what I am trying to do I appreciate guidance and constructive criticism.

EDIT

It looks like COALESCE will work for what I’m trying to accomplish. I’d like to give a little more info on exactly what I am working with and get some advice on whether I am using the best method.

I have a bloated table Table2 and it is in production. I’m working on building new web applications for this system but can’t justify a complete database redesign so I am trying to do one “on the fly”. I’ve created a new table Table1 and I am writing stored procedures for the following methods Get(Select), Set(Update), Add(Insert), Remove(Delete). This way, to my code, it will seem that I am working with a single table that is not bloated. My code will simply call one of the SP methods and then the stored procedure will handle the data between the old table and the new. I am currently working on the Get method and I need to check the old table Table2 for a record if it doesn’t exist in Table1.
Thanks to the suggestions here my query currently looks like this:

Select
    id = coalesce(a.id, b.student_number),
    first_name = coalesce(a.first_name, b.first_name),
    last_name = coalesce(a.last_name, b.last_name),
    //etc
From Table1 a
Full Outer Join Table2 b
On a.id = b.student_number
Where (a.id = @id Or b.student_number = @id)

This works for what I’m trying to accomplish, I’d like to throw it out there to the experienced crowd for any tips or suggestions if there are better or more correct ways to go about this.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:43:27+00:00Added an answer on May 18, 2026 at 11:43 am

    I suspect your problem may come from doing a left join. Try again using a full outer join, like this:

    Select 
        id = coalesce(a.id, b.id),
        name = coalesce(a.name, b.name)
    From Table1 a
    full outer Join Table2 b
    On a.id = b.id
    Where a.id = @id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a inner join between two tables where one is an association
I am doing a join on two tables. One is a user's table and
I doing a function in Javascript like the VisualBasic DateDiff. You give two dates
I have a booking system in which I need to select any available room
I have a routine that will be creating individual tables (Sql Server 2008) to
Doing odd/even styling with jQuery is pretty easy: $(function() { $(.oddeven tbody tr:odd).addClass(odd); $(.oddeven
When doing an INSERT with a lot of data, ie: INSERT INTO table (mediumtext_field)
When doing small icons, header graphics and the like for websites, is it better
When doing case-insensitive comparisons, is it more efficient to convert the string to upper
When doing TDD , how to tell that's enough tests for this class /

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.