Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If statement in SQL Server 2

Status
Not open for further replies.

Cheveliar

IS-IT--Management
Feb 4, 2003
15
US
Ratio: IIf([dmat_w_3set]+[dmat_l_3set]=0,0,([dmat_w_3set])/([dmat_w_3set]+[dmat_l_3set]))

I used this in access and now I want to traslate it to SQL but the If statement is not the same contruct and I can not find it anywhere. Any help would be appreciated.
 
SELECT IIf([dmat_w_3set]+[dmat_l_3set]=0,0,([dmat_w_3set])/([dmat_w_3set]+[dmat_l_3set])) as Ratio
...

According to my SQLServer manual, the IIF function should be of the same syntax as in Access. Switch to SQL view in your Access query. There you have (in this case) the correct syntax.
 
It's CASE you need:
Code:
SELECT CASE <expression1>
   WHEN 0 THEN <expression2> 
   WHEN 1 THEN <expression3>
   ELSE <expression4>
   END

ie when <expression1> evaluates to zero, <expression2> is selected, etc
 
There's no such thing as IIf in T-SQL. Use a CASE expression:

Code:
SELECT CASE WHEN [dmat_w_3set] + [dmat_l_3set] = 0 THEN 0 ELSE [dmat_w_3set] / ([dmat_w_3set] + [dmat_l_3set]) END AS Ratio

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top