SQL Server’s Equivalent to Sleep(): The WAITFOR Statement

In SQL Server, you can use the WAITFOR statement to delay the execution of a batch, stored procedure, or transaction.

It works similar to MySQL‘s sleep() function.

Actually, it’s probably more like a combination of Postgres‘s three “sleep” functions: pg_sleep(), pg_sleep_for(), and pg_sleep_until().

I say this, because SQL Server’s WAITFOR statement gives you the option of specifying either a time delay, or an actual fixed time before execution continues.

Continue reading

4 Ways to Replace NULL with a Different Value in MySQL

In MySQL, sometimes you don’t want NULL values to be returned as NULL. Sometimes you want NULL values to be returned with a different value, such as “N/A”, “Not Applicable”, “None”, or even the empty string “”.

Fortunately there are several ways to do this in MySQL.

Here are four:

  • The IFNULL() function
  • The COALESCE() function
  • The IF() function combined with the IS NULL (or IS NOT NULL) operator
  • The CASE expression combined with the IS NULL (or IS NOT NULL) operator

Examples of these options are below.

Continue reading