• Hi David,

    I am aware that Books Online calls CASE a "function". This is incorrect, though, for the following two reasons.

    1. Functions always take the form functionname(argument, ...). The CASE expression has a completely dissimilar form.

    2. The SQL standards that define the SQL language have defined CASE as an expression. I just doublechecked this in both the ANSI-92 and the SQL-2003 standards, and my memory didn't play any tricks on me - both versions of the standard list CASE as an expression.

    The fact that CASE takes expressions for input is nothing special, as all expressions and all functions take expressions for input. Your statement that CASE produces an expression for output is incorrect; the output of a CASE expression is a scalar value, just as the output of a function.

    This being said, I'll be the first to admit that the difference between expressions and functions is even more nitpicky than the difference between expressions (or functions) and statements. I don't see any real need to distinguish expressions from functions (in fact, functions are a subset of expressions), whereas there is a real difference between expression (and hence functions) on the one hand and statements on the other hand.

    A statement can be used on it's own, and can not be embedded in other statements or expresions on a place where an expression is expected (examples: IF, GRANT, SELECT, UPDATE, ...). Both expressions and functions can not be used on their own, but only as part of a statement or expression, in any location where an expression is expected (examples: DATEDIFF, CASE, (SELECT ...), CURRENT_USER, ...).


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/