How IIF() Works in SQL Server

In SQL Server, the IIF() function (not to be confused with the IF statement) is a conditional function that returns the second or third argument based on the evaluation of the first argument.

It’s a shorthand way for writing a CASE expression. It’s logically equivalent to CASE WHEN X THEN Y ELSE Z END assuming IIF(X, Y, Z).

IIF() is an abbreviation for Immediate IF.

Continue reading

How the IF Statement Works in SQL Server

Most (probably all) programming languages include some form of the IF statement that allows programmers to write conditional code. That is, code that will execute only if a certain condition is true.

It’s a very simple concept. Basically it goes like this:

“If this, do that.”

Most languages simply call it IF, but some have their own twist on the name (for example, in ColdFusion/CFML, it’s called CFIF).

In any case, they essentially do the same thing.

In SQL Server (or more precisely, its programming language T-SQL) it’s called IF.

Continue reading