Click here to Skip to main content
15,887,875 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
CREATE TABLE #A(ID INT,NAME VARCHAR(10))
INSERT INTO #A VALUES(1,'ABC')
INSERT INTO #A VALUES(2,'XYZ')
INSERT INTO #A VALUES(3,'mnc')


SQL
CREATE TABLE #B(ID INT,NAME VARCHAR(10))
INSERT INTO #B VALUES(null,'ABC')
INSERT INTO #B VALUES(2,'XYZ')
INSERT INTO #B VALUES(3,'mnc')



SQL
Select * from #A where ID not in (select ID from #B)


In This I am not getting any result.
Actually the result Came is "1,ABC" But I am not getting result
Posted
Updated 20-Jul-12 18:01pm
v2
Comments
[no name] 20-Jul-12 4:41am    
Try it with full qualified names:
Select #A.* from #A where #A.ID not in (select #B.ID from #B)
JakirBB 20-Jul-12 6:18am    
Why should this work?
[no name] 20-Jul-12 7:33am    
What do you mean that you are getting the result but not getting result?

SQL
CREATE TABLE #A(ID INT,NAME VARCHAR(10))
INSERT INTO #A VALUES(1,'ABC')
INSERT INTO #A VALUES(2,'XYZ')
INSERT INTO #A VALUES(3,'mnc')
 
CREATE TABLE #B(ID INT,NAME VARCHAR(10))
INSERT INTO #B VALUES(null,'ABC')
INSERT INTO #B VALUES(2,'XYZ')
INSERT INTO #B VALUES(3,'mnc')
 
Select * from #A where ID not in (select ID from #B WHERE ID IS NOT NULL)

DROP TABLE #A
DROP TABLE #B
 
Share this answer
 
Hi...

U can use join for the above one.


SQL
select  #A.ID,#A.NAME   from #A left  join #B 
on #A.ID=#B.ID where #B.ID is null
 
Share this answer
 
Comments
JakirBB 20-Jul-12 8:14am    
Yeah! Joining is better.
SQL
Select * from #A where  Id not in (select isnull(ID,0) from #B )


or
SQL
Select * from #A where  Id not in (select Id from #B where ID is not null )
 
Share this answer
 
SQL
Select * from #A where ID not in
 (select isnull(ID,0) from #B)



You can use isnull function for this kind of situations
 
Share this answer
 
SQL
Select A.* from #A A ,#B B 
 where A.ID not in (B.ID)
 
Share this answer
 

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