I think this could function properly....select * from #t1 where 1 = (case when ididivision=100 and idcompany=1 or iddivision=240 and data between @beginLastYear and @endLastYear then 1
when (ididivision<>100 and idcompany=1) and iddivision<>240 and data between @beginThisYear and @endThisYear then 1
end)