Click here to Skip to main content
15,920,031 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hai freinds i have a prob ,,,


i have 2 datatables dt1,dt2


dt1 contains
A
B
C
D



and dt2

contain

A
B
C
D
E

I want

only E

how to get plz help
Posted
Comments
V.Lorz 9-Sep-13 9:05am    
How do you have the tables? Do you have two independent instances of DataTable, two DataTables contained in one Single DataSet, or two tables in one database? Each case requires it's own solution. The solution for the third one was given by RyanDev in Solution 1.

SQL
SELECT *
FROM dt2
WHERE field1 NOT IN (SELECT field1 FROM dt1)


This will give you all values from table 2 that are not in table 1.
 
Share this answer
 
Comments
mrbusy52 23-Oct-13 8:30am    
it is posssible i have done using hash table
ZurdoDev 23-Oct-13 8:39am    
Is this supposed to mean something?
DataTable dt3=new DataTable ();
dt3.Columns.Add("Name");
for (int i = 0; i < dt1.Rows.Count; i++)
{
for (int j = 0; j < dt2.Rows.Count; j++)
{
if (dt1.Rows[i][0].ToString() == dt2.Rows[j][0].ToString())
{

}
else
{
DataRow dr = dt3.NewRow();
dr["Name"] = dt1.Rows[i][0].ToString();
dt3.Rows.Add (dr);
}
if (dt2.Rows[i][0].ToString() == dt1.Rows[j][0].ToString())
{

}
else
{
DataRow dr = dt3.NewRow();
dr["Name"] = dt2.Rows[i][0].ToString();
dt3.Rows.Add(dr);
}
}
}




you will get all your data in datatable dt3
 
Share this answer
 
This example identifies values that are missing from either table instead of values that are missing from just one table. To select only items that are missing from Table_1, change the word Full to Left.

Find mismatched
SQL
With CTE (C1,C2) As
 (
 SELECT T1.Col1 As C1,T2.Col1 As C2 FROM Table_2 T2  full join Table_1 T1 On T2.Col1=T1.Col1
 ) 
 Select ISNULL(C1,C2) As Col1 from CTE where C1 Is Null Or C2 Is Null Order By ISNULL(C1,C2)


Results
Col1
E
F


Create test data
SQL
CREATE TABLE [dbo].[Table_1](
    [col1] [nchar](10) NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Table_2](
    [col1] [nchar](10) NULL
) ON [PRIMARY]
GO
insert into table_1 values ('A');
insert into table_1 values ('B');
insert into table_1 values ('C');
insert into table_1 values ('D');
insert into table_1 values ('F');

insert into table_2 values ('A');
insert into table_2 values ('B');
insert into table_2 values ('C');
insert into table_2 values ('D');
insert into table_2 values ('E');
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900