Tip of the Day : Example Uses of the ISNULL System Function

Welcome to SQL Server Helper !!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

SQL Server Tip of the Day - February 25, 2018

Example Uses of the ISNULL System Function

The ISNULL system function replaces NULL with the specified replacement value.  The return data type is the same type as the first parameter.  The syntax of the ISNULL system function is as follows:

ISNULL ( <check_expression>, <replacement_value> )

The <check_expression> parameter is the expression to be checked for NULL and can be of any type.  The <replacement_value> parameter is the expression to be returned if <check_expression> is NULL.  The <replacement_value> must be of a type that is implicitly convertible to the type of the <check_expression> parameter.  The value of <check_expression> is returned if it is not NULL; otherwise, the value of the <replacement_value> is returned after it is implicitly converted to the type of the <check_expression> parameter, if the types are different.

Here are a few uses of the ISNULL system function:

Usage #1 : Generate a Comma-Delimited String

DECLARE @CSV     VARCHAR(MAX)

SELECT @CSV = ISNULL(@CSV + ',', '') + [UserName]
FROM [dbo].[Users]

Usage #2 : Get Full Name from an Optional First Name and an Optional Last Name

SELECT ISNULL([LastName] + ', ', '') + ISNULL([FirstName], '') AS [FullName]
FROM [dbo].[Users]

Usage #3 : Listing NULL Values at the End of an Ordered Result Set

SELECT [FirstName], [LastName], [Salary]
FROM [dbo].[Employees]
ORDER BY ISNULL([Salary], 9999999) ASC

Usage #4 : Return the First Non-NULL Value from a List of Columns

SELECT ISNULL(ISNULL([PhoneNumber], [CellNumber]), [OfficeNumber]) AS [Contact Number]
FROM [dbo].[Customer]

SQL Server 2012

SQL Server 2008

User-Defined Functions

Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions

A collection of useful table-valued user-defined functions that can be used to join with other tables.

SQL Server Built-in Functions

A reference to all built-in functions available within SQL Server grouped into categories.

Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

Frequently Asked Questions