Collections:
ISNULL() - Replacing NULL Values in Expressions in SQL Server
How To Replace NULL Values in Expressions using ISNULL() in SQL Server Transact-SQL?
✍: FYIcenter.com
As you learned from previous tutorials, NULL values presented in expressions will cause the final results to be NULL. Sometimes, you want NULL values to be replaced with some default values, like 0, '', or 'NULL', so that expressions can be evaluated properly.
SQL Server offers a built-in function called ISNULL() to help you replacing NULL values in expressions:
ISNULL(expression, replacement) -- Returns "expression", if it is not NULL -- Returns "replacement", if "expression" is NULL
The tutorial example below shows you how to replace possible NULL values in @middle_initial:
USE FyiCenterData; GO CREATE PROCEDURE welcome @first_name VARCHAR(20), @middle_initial VARCHAR(1), @last_name VARCHAR(20) AS PRINT 'Hello '+@first_name + ' '+@middle_initial + ' '+@last_name; PRINT 'Hello '+@first_name + ' '+ISNULL(@middle_initial,'') + ' '+@last_name; GO EXEC welcome 'John', 'W', 'King'; GO Hello John W King Hello John W King EXEC welcome 'John', NULL, 'King'; GO Hello John King
The first PRINT statement in the second test returns a blank line because of NULL value.
⇒ NULLIF() - Replacing Given Values with NULL in SQL Server
⇐ "IS NULL" - Testing NULL Values in SQL Server
2017-02-03, 2951🔥, 0💬
Popular Posts:
How To List All Stored Procedures in the Current Database in SQL Server Transact-SQL? If you want to...
Where to find answers to frequently asked questions on Managing Security, Login and User in SQL Serv...
Where to find answers to frequently asked questions on Conditional Statements and Loops in SQL Serve...
How To List All Login Names on the Server in SQL Server? If you want to see a list of all login name...
How AdventureWorksLT tables are related in SQL Server? There are 12 user tables defined in Adventure...