SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Searching for a string in two tables using joins


Searching for a string in two tables using joins

Author
Message
Minnu
Minnu
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1355 Visits: 950
Hi Team,


I have two tables,

table 1 : Test1
ColA | Value
----------------------
A 100





Table 2 : Test2
ColA | Value
-----------------------
B 200


if COLA =A is available in Table1 then
COLA : A Value: 100

else
check in Table2, if available
COLA : A Value: 100


Searching for a string in two tables using joins


A : 300
B : 200
c : 300
d : 150


Yes, Using joins..

First it need to check in table1, if value found then assign that values to a string,
if not found then only it should check in table2

for Table1 am using below code
---------------------------------

SELECT RTRIM(
+ 'ColA:'+ISNULL(ColA,'')+', '
+ ISNULL('ColB:'+CAST(ColB AS VARCHAR(10))+' ','')
+ ISNULL('ColC:'+CAST(ColC AS VARCHAR(10))+' ','')
+ ISNULL('ColD:'+CAST(ColD AS VARCHAR(10))+' ','')
)
FROM



for table 2 am using below code:
---------------------------------------

select @query1 = (SELECT
CASE WHEN Col1 IS NULL or Col1 = '' THEN ''
ELSE CASE WHEN Value IS null then
'Col1: ' + rtrim(Col1) +CHAR(10)
ELSE 'Col1: ' +rtrim(usage_rights) + ' (Col2 ' + rtrim(convert(varchar, Value,107)) + ')' +CHAR(10)
END
END +




i want a final code by joining two tables and assign a final string to a variable @query1

Plz Help
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17002 Visits: 7413
How about this?
create table #T1 (id char(1), val int)
create table #T2 (id char(1), val int)
insert #T1 values('A',100),('C',300)
insert #T2 values ('B',200),('D',500),('E',400)

SELECT COALESCE(#T1.id, #T2.id) AS ID,
COALESCE(#T1.val, #T2.val) AS Val
FROM #T1
FULL OUTER JOIN #T2 ON #T1.id = #T2.id




____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62123 Visits: 17954
Isn't this the same question you posted yesterday??

http://www.sqlservercentral.com/Forums/Topic1391849-391-1.aspx

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search