Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm curious if it's possible to add a condition inside the from table part.

What I would need is something like this select * from table A inner join on table B on if(B.column = "this") A.column = B.Column1 else A.column = B.column2.


The entire query consists of to CTE's and I can probably work around this by adding a third one in to the final selection but I was curious if it was possible doing it like this.
Posted
Updated 1-Sep-15 23:13pm
v2

Hello ,
You may try like this way

SQL
select * from table A inner join  table
B on A.column= case when B.column ='this'  then B.Column1   else  B.column2 end
 
Share this answer
 
Comments
Member 11683251 2-Sep-15 8:57am    
Thanks that was exactly what I was looking for. Much appreciated.
Animesh Datta 3-Sep-15 1:40am    
glad to help you :)
Check this out Conditional Joins in SQL Server[^] the example nearing the end should help.
 
Share this answer
 
Comments
Member 11683251 2-Sep-15 8:58am    
Thanks for the help, love it when something is easily done in sql as opposed to the twists and turns I've had to go through to make some queries behave as I want them to.

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