SQL Server IF vs IIF(): What’s the Difference?

Students learning a new programming language will often start their first lesson with an IF statement, where their statement will return a value only if the expression is true.

They might then progress to an IF... ELSE statement, where they can determine another value to return if the expression is false. So therefore, return one value if the expression is true, and another value if it’s false.

SQL Server certainly includes the IF... ELSE statement in its T-SQL toolbox.

SQL Server also includes the IIF() function, which does a similar thing, but with a more concise syntax.

But there are some subtle differences.

Continue reading

How to Pause the Execution of a Statement in PostgreSQL

PostgreSQL includes three functions that allow you to delay the execution of the server process. the execution of a statement.

In other words, you can run a statement and have it pause half way through, before continuing on its merry way.

The three functions are:

These are all very similar, but they work in slightly different ways.

Below are examples of each one.

Continue reading

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