IsEmpty function like ISNULL in SQL Server? – Sql

Photo of author
Written By M Ibrahim
android-sqlite sql-server-2005 sql-server-2008-r2 t-sql

Quick Fix: Use NULLIF() function to check and return NULL value if the specified SKU is an empty string.

The Problem:

Develop a SQL function called IsEmpty that emulates the functionality of the ISNULL function, which is frequently utilized in SQL Server. When provided with a variable, the IsEmpty function should determine if it is null or an empty string. If the variable is empty or null, it should return a specified default value; otherwise, it should return the original variable’s value.

The Solutions:

Solution 2: Using NULLIF and ISNULL

You can use a combination of the `NULLIF()` and `ISNULL()` functions to achieve the desired functionality. Here’s an example:

SET @YourValue = ISNULL(NULLIF(@YourValue, ''), NULL);

In this example, the `NULLIF()` function is used to check if the value of `@YourValue` is equal to an empty string (`”`). If it is, `NULLIF()` returns `NULL`. Otherwise, it returns the value of `@YourValue`. The `ISNULL()` function is then used to check if the result of the `NULLIF()` function is `NULL`. If it is, `ISNULL()` returns the value of the second argument (which is `NULL` in this case). Otherwise, it returns the result of the `NULLIF()` function.

As a result, this expression will return `NULL` if the value of `@YourValue` is `NULL`, an empty string, or a space. Otherwise, it will return the value of `@YourValue`.

This approach is more concise and easier to read than the series of `IF` statements you are currently using.

Solution 3: Use `SET @SKU = NULLIF(@SKU,'')` to set @SKU to null where @SKU equals the value of the second argument.

`IsEmpty` isn’t a built-in T-SQL function, but `NULLIF` can be used to achieve a similar behavior.
`NULLIF` takes two arguments: the value to check for null, and the value to return if the first argument is null. In this case, we are checking the value of `@SKU` and returning `NULL` if it is empty (equals the empty string). This allows us to set `@SKU` to null when it is empty, while leaving it unchanged if it contains a non-empty string.

Solution 5: Using ISNULL and NULLIF Functions

In SQL, you can use the ISNULL and NULLIF functions to achieve a similar functionality to the IsEmpty function. Here’s how you can use these functions:

In the SELECT clause:

SELECT ISNULL(nullif(EmptyOrNullOrBlankColumn,''),NULL) AS ResultColumn

In the WHERE clause:

WHERE ISNULL(nullif(EmptyOrNullOrBlankColumn,''),NULL) is not null

Explanation:

  1. NULLIF(EmptyOrNullOrBlankColumn, ''): This expression checks if the EmptyOrNullOrBlankColumn is empty or blank (equal to an empty string). It returns NULL if the column is empty or blank, otherwise, it returns the value of the column.
  2. ISNULL(nullif(EmptyOrNullOrBlankColumn, ''), NULL): This expression further checks if the result of the NULLIF expression is NULL. If it is NULL, it returns NULL, otherwise, it returns the value of the NULLIF expression.
  3. Using this expression in the SELECT clause allows you to retrieve only non-empty or non-blank values for the ResultColumn.
  4. Using this expression in the WHERE clause allows you to filter rows where the EmptyOrNullOrBlankColumn is not empty or blank.

This approach provides a concise and efficient way to handle empty or blank values in your SQL statements, similar to the IsEmpty function in other programming languages.