Can i use IF statement in this query?
Hi I am wondering if i can use IF statement in the following query..
select * from Table
Where Field1 = 'john' and
if Field2 = 'Mike'
Then(dbo.StateId(gp.SiD) Like '%' + @StateName +'%开发者_运维问答'))
Else Nothing**
Thanks
No.
SELECT *
FROM Table
WHERE Field1 = 'john'
AND ( Field2 <> 'Mike'
OR dbo.StateId(gp.SiD) LIKE '%' + @StateName + '%' )
(Your sample code doesn't make clear to me what should happen if Field2
is NULL
)
I think SELECT/CASE will work for what you are trying to do
select *
from Table
Where Field1 = 'john'
and case when Field2 = 'Mike'
Then (dbo.StateId(gp.SiD) Like '%' + @StateName +'%'))
Else ( true or false based on inclusion ) end
精彩评论