how to use if condition in sql query [Resolved]

Posted by hareeshkumarkr-16190 under Sql Server on 9/30/2013 | Points: 10 | Views : 679 | Status : [Member] | Replies : 2
Hi,

I have posted my query below. i want test the condition like
(if (t1.accid=t1.aid) then 1 else 0 )



regards
harish




Responses

Posted by: Bandi on: 9/30/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
(if (t1.accid=t1.aid) then 1 else 0 ) 

equivalent in SQL Server is as follows:

SELECT CASE WHEN t1.accid=t1.aid THEN 1 ELSE 0 END AS flag
FROM TableName t1


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

hareeshkumarkr-16190, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/30/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
If you have SQL Server 2012, then you can do as follows:
SELECT IIF( t1.accid=t1.aid, 1 , 0 )
FROM TableName T1


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

hareeshkumarkr-16190, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response