IIF-function gives errors

  • Folks,

    I am using SQL Server 2012 Express Edition and want to make use of the new IIF-function. I am using the following code:

    IIF(Rank=1,'Gold',IIF(Rank =2,'Silver', 'Bronze'))

    Rank is a databasefield.

    My problem is that (when working in the criteria pane of Studio) I am getting the following error:

    An expression of non-boolean type specified in a context where a condition is expected, near '('

    It looks like IIF is not recognized??

    Working in de sql pane of Studio the error is something like:

    Error in list of function arguments: '=' not recognized.

    Unable to parse query text..

    I have tried almost everything to get the problem solved, but nothing seems to work.

    Can anyone get me out of this mess?

    Grz,

    Robert

  • no longer applicable

  • IIF is a function that is used to return a value. Just like other functions (for example getdate()), it can't be written on its own without setting it's value to a variable or without select statement that is using it. See my code example:

    create table MyTable (Rank int);

    insert into MyTable values (1),(2),(3);

    select IIF(Rank=1,'Gold',IIF(Rank =2,'Silver', 'Bronze'))

    from MyTable;

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Adi,

    Thanks: everything is working well now. I apologize for the mixing up the threads.

    Grz,

    Robert

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply