'Does the SQL Server IIF function accept defined column values?

I am trying to use the IIF function and would like the parameters value_if_true and value_if_false to be defined by a computed column value. More generally, can I use computed column values as function parameters?

Here is a greatly simplified (failing case):

select datepart(second,table.date1) as d1,
       datepart(second,table.date2) as d2,
       IIF(d1>d2,date1,date2) as biggerdate

This results in d1 and d2 as invalid column names. Obviously I could replace d1 and d2 with their computed definitions but the actual sql query could be greatly simplified if the computed values could be used.I was expecting the computed values to work as parameters in the IIF.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source