laika222
4/6/2017 - 7:08 PM

How to test if a cell IS NULL or IS NOT NULL.

How to test if a cell IS NULL or IS NOT NULL.

-- How to check if a value IS NULL using a SELECT statement - if the value IS NULL, a value of 1 will be returned. If the value IS NOT NULL, a value of 0 will be returned.
SELECT PostalCode IS NULL FROM Customers2 WHERE CustomerID = 432;

-- How to check if a value IS NOT NULL using a SELECT statement - if the value IS NOT NULL, a value of 1 will be returned. If the value IS NOT NULL, a value of 0 will be returned.
SELECT PostalCode IS NOT NULL from Customers2 WHERE CustomerID = 432;


-- How to check if a value IS NULL or IS NOT NULL within an IF ELSE statement. This example check whether the local variable seconds_since IS NULL.
IF seconds_since IS NULL THEN SELECT 'OrderID Does Not Exist' AS 'Error';
ELSE SELECT (seconds_since * -1) AS 'Seconds Since Order'
  , minutes_since AS 'Minutes Since Order'
  , hours_since AS 'Hours Since Order'
  , days_since AS 'Days Since Order'
  , years_since AS 'Years Since Order';
END IF;