Ternary operator


  • Is there a ternary operator which can be used in a stored procedure ?

    Something like this:  Iff ( @i > 0, SELECT CategoryID ... ,   SELECT CustomerID ...  )

    Any examples?



    Saturday, December 09, 2006 6:14 AM


All replies

  • Use


    read more about it from the BOL

    Gurpreet S. Gill
    Saturday, December 09, 2006 6:45 AM
  • You can use CASE like this.

    for more info refer to here

    Friday, October 18, 2013 9:00 AM
  • declare @i int=0
    --Set  @i =1
    create table test(id int,col1 varchar(1000),col2 varchar(50))
    insert into test values (1,'v1','t1')
    -----SQL Server 2012
    --SELECT  IIf ( @i > 0, col1,col2) as col from test
    ---Other SQL Server versions
    SELECT  CASE WHEN  @i > 0 THEN  col1 ELSE col2 END  as col from test
    drop table test

    Friday, October 18, 2013 3:25 PM
  • IIF will work only in SQL Server 2012 version. for other versions you can use CASE WHEN THEN to apply the condition and select a value based on that condition. To know more about IIF function check this msdn library article

    Rajanand Ilangovan | ~Mark as answered if my post solved your problem and Vote as helpful if it was useful.~

    Friday, October 18, 2013 8:36 PM