Click here to Skip to main content
15,920,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Friends,

I have 5 states like,

1 Andhra Pradesh
2 Assam
3 Karnataka
4 Tamilnadu
5 Andaman and Nicobar Islands


I want to select the required states using intersect & except.

My expected output is like,

Output 1:

Andhra Pradesh
Andaman and Nicobar Islands


Output 2:

Assam
Karnataka
Tamilnadu

Note: using except & intersect are mandatory

Please help me.

Thanks in advance.
Posted
Comments
Tomas Takac 18-Nov-14 7:39am    
Well to do intersect or except you need two sets. So you can do Input INTERSECT Output 1 to get the first result and Input EXCEPT Output 1 to get second result.
Member 10021658 18-Nov-14 7:45am    
Dear Tomas,

Thanks for your reply. I have already tried using these query

select * from tblStates intersect select * from tblStates where StateName='Andhra Pradesh' or StateName='Andaman & Nicobar Islands'

But I got the output Andhra Pradesh only, not both.
Member 10021658 18-Nov-14 7:47am    
Same thing using except like,

select * from tblStates except select * from tblStates where StateName='Andhra Pradesh' or StateName='Andaman & Nicobar Islands'

I have got the output all states except Andhra pradesh.
Tomas Takac 18-Nov-14 7:55am    
In your question is says the state is named 'Andaman and Nicobar Islands' not 'Andaman & Nicobar Islands'. Try to fix that.
Member 10021658 18-Nov-14 7:59am    
I got the output by using StateId's.

Thanks.

1 solution

Why? And based on what condition?

Did you look at the documentation?

http://msdn.microsoft.com/en-us/library/ms188055.aspx[^]

You use EXCEPT or INTERSECT when you have atleast two queries and you want to produce a result based on those two queries.
 
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