Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « A Quick look at the new EOMONTH function in SQL Server Denali CTP3SQL Server Code Name "Denali" CTP3 is available for download »
    comments

    Denali CTP3 comes with the IIF function, if you have used VB or Excel then you already know how this function works. In essence this function is a shorter version of a CASE statement.

    The syntax is as follows

    IIF ( boolean_expression, true_value, false_value )

    So instead of this

    1. SELECT CASE WHEN 1 = 2 THEN 'equal' ELSE 'not equal' END AS Comp

    We can do this

    1. SELECT IIF(1=2,'equal','not equal') as Comp

    Both of those will return not equal

    Be aware that you can't use NULL like in the example below

    1. SELECT IIF(1=2,NULL ,NULL ) as calc

    It throws an error (with a typo)

    Msg 8133, Level 16, State 1, Line 1
    At lease one of the result expressions in a CASE specification must be an expression other than the NULL constant.

    If you use a variable then you can use NULL

    1. declare @i int = NULL
    2.  
    3.  
    4. SELECT IIF(1=2,@i,@i) as calc

    Here is some more info from Books On Line

    IIF is a shorthand way for writing a CASE statement. It evaluates the Boolean expression passed as the first argument, and then returns either of the other two arguments based on the result of the evaluation. That is, the true_value is returned if the Boolean expression is true, and the false_value is returned if the Boolean expression is false or unknown. true_value and false_value can be of any type. The same rules that apply to the CASE statement for Boolean expressions, null handling, and return types also apply to IIF.

    The fact that IIF is translated into CASE also has an impact on other aspects of the behavior of this function. Since CASE statements can nested only up to the level of 10, IIF statements can also be nested only up to the maximum level of 10. Also, IIF is remoted to other servers as a semantically equivalent CASE statement, with all the behaviors of a remoted CASE statement.

    Here is a nested (silly) example

    1. SELECT IIF(1=2,'equal',IIF(4=2,'equal','not equal')) as Comp

    Here is another example that combines IIF with TRY_CONVERT to return if a value can be converted to a specific data type

    1. SELECT IIF(TRY_CONVERT(float,'bla')IS NULL,'Cast failed','Cast succeeded')
    2. UNION
    3. SELECT IIF(TRY_CONVERT(float,'1')IS NULL,'Cast failed','Cast succeeded')

    --------
    Cast failed
    Cast succeeded




    I welcome this function, anything that makes the code shorter is welcomed with open arms by me.

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    1257 views
    denali, functions, iif
    Instapaper

    7 comments

    Comment from: Ted Krueger (onpnt) [Member]
    Ted Krueger (onpnt) How was your speed compared to CASE, Denis? Pretty much the same or did the new IIF seem to increase or lower overall performance on a large volume of data and its use?
    07/13/11 @ 09:41
    Comment from: SQLDenis [Member] Email
    SQLDenis Didn't do that test yet
    07/13/11 @ 09:42
    Comment from: Naomi Nosonovsky [Member]
    Naomi Nosonovsky Can you show syntax for IIF with more than 2 expressions? Will it be nested? As in other languages IIF can only have 1 true and one false.
    07/13/11 @ 09:44
    Comment from: SQLDenis [Member] Email
    SQLDenis I added the nested example


    SELECT IIF(1=2,'equal',IIF(4=2,'equal','not equal')) as Comp
    07/13/11 @ 09:55
    Comment from: David Forck (thirster42) [Member]
    interesting. i'll probably stick with the case statements unless it's a IF(a=1,true,false) sort of thing because otherwise the case statements are more flexable it seems like.

    example: case blah when 1 then 'a' when 2 then 'b' when 3 then 'c' else 'd' end

    iif(blah=1,'a',iif(blah=2,'b',iif(blah=3,'c','d')))
    07/13/11 @ 10:25
    Comment from: SQLDenis [Member] Email
    SQLDenis I can't wait to for the person who will mix nested case with nested IIFs....that is going to be a total mess
    07/13/11 @ 11:40
    Comment from: vince [Member]
    vince Mysql was proposing it for ages, it was definitely lacking in SQL Server. Great they thought about it for this release.
    Thanks for your time writing those functions dedicated articles Denis !
    07/15/11 @ 06:59

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)