Question

Can Somebody help me with following sql query.

When you want to filter by Phase I, It should return results with Phase I or Phase I/II but not Phase II or Phase III.

Similarly for Phase II, it should return results with Phase I/II, Phase II or Phase II/III

select Phase from Phases where Phase in (case 

when @phase='Phase I' then '''Phase I'',''Phase I/II'''
when @phase='Phase II' then '''Phase I/II'',''Phase II'',''Phase II/III'''
end)

I do not want to use dynamic query to solve the issue.

Was it helpful?

Solution

Boolean logic can solve this:

select Phase 
  from Phases 
 where (@phase = 'Phase I'  and Phase in ('Phase I', 'Phase I/II'))
    or (@phase = 'Phase II' and Phase in ('Phase I/II', 'Phase II', 'Phase II/III'))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top