SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Selecting values from 2 tables, display 1 result
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

iNko
Starting Member

Lithuania
19 Posts

Posted - 01/22/2013 :  10:17:16  Show Profile  Reply with Quote
Hey, i need help with a code that would take a values from 2 columns, and show only 1 result if theres more than 1..

Table1
ID-Name
1-Name1
2-Name2

Table2
ID-Name
1-Name1
5-Name5

I want something like this:
SELECT name FROM Table1 OR Table2 WHERE id='1'

Also when it returns (Name1, Name1) result, i would want it to be just (Name1).

Can someone help me with this? ty.

James K
Flowing Fount of Yak Knowledge

3742 Posts

Posted - 01/22/2013 :  10:57:33  Show Profile  Reply with Quote
SELECT
	COALESCE(a.id,b.id) AS id,
	COALESCE(a.Name, b.Name) AS NAME 
FROM
	Table1 a
	FULL JOIN Table2 b ON 
		a.id = b.id;
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 01/23/2013 :  01:00:35  Show Profile  Reply with Quote

SELECT Name FROM Table1
UNION
SELECT Name FROM Table2


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000