SQL Server ISNULL() Function
Last Updated :
02 Sep, 2024
The ISNULL()
function in SQL Server is a powerful tool for handling NULL
values in our database queries. It allows us to replace NULL
values with a specified replacement value, ensuring that your queries return meaningful results even when data is missing.
In this article, We will learn about the SQL Server ISNULL() Function by understanding various examples with output and explanation.
SQL Server ISNULL() Function
- The
ISNULL()
function in SQL Server is used to replace NULL
values with a specified replacement value.
- It takes two arguments which are an expression to evaluate and the replacement value if the expression is
NULL
.
Syntax
SQL Server ISNULL() function syntax is:
ISNULL(expression, value)
Parameter :
This method accepts two parameters.
- expression: The specified expression is to be checked to see if it's NULL or not.
- value: The specified value to be returned, in case the expression is NULL.
Example of SQL Server ISNULL() Function
Let's look at some examples of the ISNULL function in SQL Server. Learning the ISNULL function with examples will help in understanding the concept better.
Example 1
Suppose we want to check if a specific string value is NULL
and, if so then replace it with a default value.
SELECT ISNULL('gfg', 'Geeks');
Output:
gfg
Explanation: In the query SELECT
ISNULL('gfg', 'Geeks');
, since the first argument 'gfg'
is not NULL
, the function returns 'gfg'
without using the replacement value '
Geeks
'
.
Example 2
Write a query to replace a NULL
value with a specified string in SQL Server.
SELECT ISNULL(NULL, 'Geeks');
Output :
Geeks
Explanation: The query replaces the NULL
value with the string 'Geeks'
, so the output will be 'Geeks'
.
Example 3
Using ISNULL() function and getting the output using a variable.
DECLARE @exp VARCHAR(50);
SET @exp = 'geeksforgeeks';
SELECT ISNULL(@exp, 150);
Output :
geeksforgeeks
Example 4
Suppose we need to check if a variable containing a string value is NULL
and return a specific value if it is.
DECLARE @exp VARCHAR(50);
DECLARE @val VARCHAR(50);
SET @exp = NULL;
SET @val = 'GFG';
SELECT ISNULL(@exp, @val);
Output:
GFG
Explanation:
The query declares a variable @exp
with the value 'geeksforgeeks'
and uses the ISNULL()
function to check if @exp
is NULL
. Since @exp
is not NULL
, the original value 'geeksforgeeks'
is returned, and the replacement value 150
is ignored.
Important Points About SQL Server ISNULL Function
- The ISNULL() function is used to return a specified value if the expression is NULL, otherwise it returns the expression itself.
- It is used to replace NULL values in expressions or table columns with a meaningful value.
- It is different from the IS NULL operator, which is used to check if a value is NULL.
- The ISNULL() function is useful for handling NULL values in queries and providing alternative values when NULL is encountered.
Conclusion
In conclusion, the ISNULL()
function is an essential feature in SQL Server for dealing with NULL
values. By providing a way to replace NULL
with a predefined value, it helps maintain data integrity and ensures that your queries produce consistent and accurate results. Whether you're working with variables or table columns, mastering the ISNULL()
function will enhance your ability to handle NULL
values effectively in your SQL Server queries.
Similar Reads
NULLIF() Function in SQL Server
NULLIF() function in SQL Server is used to check if the two specified expressions are equal or not. If two arguments passed to a function are equal, the NULLIF() function returns Null to us. NULLIF() function operates like a Like a CASE Statement. It will return the value of the first argument if th
3 min read
LOG() Function in SQL Server
The LOG() function returns the logarithm of a specified number or the logarithm of the number to the specified base. Syntax : LOG(number, base) Parameter : LOG() function accepts two-parameters as mentioned above and described below. number - This parameter hold a number which is greater than 0. bas
1 min read
SQL Server LEN() Function
SQL SERVER LEN() function calculates the number of characters of an input string, excluding the trailing spaces. LEN() Function in SQL ServerThe LEN function in the SQL Server fetches the number of characters in a string. It counts the preceding spaces but not the trailing spaces. For eg, 'SQL SERVE
2 min read
IIF() Function in SQL Server
IIF() function judges or evaluates the first parameter and returns the second parameter if the first parameter is true, otherwise, it returns the third parameter. IIF() function used in SQL Server to add if-else logic to queries.IIF is not supported in dedicated SQL pools in Azure Synapse Analytics.
2 min read
SUM() Function in SQL Server
The SUM() function in SQL Server is an essential aggregate function used to calculate the total sum of values in a numeric column. It aggregates data by summing up all values in the specified column for the rows that match the criteria of the query.In this article, We will learn about SUM() Function
3 min read
AVG() Function in SQL Server
The AVG() function in SQL Server is an essential aggregate function used to compute the average value of a numeric column. It works by summing all non-NULL values in the specified column and then dividing the total by the number of these values. In this article, We will learn about AVG() Function in
3 min read
MIN() Function in SQL Server
MIN() : This function in SQL Server is used to find the value that is minimum in the group of values stated. Features : This function is used to find the minimum value.This function comes under Numeric Functions.This function accepts only one parameter namely expression. Syntax : MIN(expression) Par
2 min read
DAY() Function in SQL Server
DAY() function : This function in SQL Server is used to return the day of the month i.e, from 1st to 31st for date stated. Features : This function is used to find the day of the month for a date specified. This function comes under Date Functions. This function accepts only one parameter i.e, date.
2 min read
LTRIM() Function in SQL Server
The LTRIM() function in SQL Server removes all the space characters found on the left-hand side of the string. It removes the leading spaces from a string, SyntaxThe LTRIM function for SQL Server syntax is: LTRIM(string, [trim_string]) Parameter: string - The string from which the leading space char
2 min read
Scalar Function in SQL Server
Pre-requisites: Categories of SQL Functions In SQL Server, a scalar function is a type of user-defined function that returns a single scalar value based on the input parameters passed to it. The scalar function is used to perform some calculations or operations on the input parameters and return a s
2 min read