Open In App

SQL Server NULL Values

Last Updated : 04 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In SQL Server, NULL represents the absence of a value in a column. It signifies missing or undefined data, distinct from zero or an empty string. SQL Server uses a three-valued logic, and handling NULL is crucial for accurate querying and reporting. Conditions like IS NULL and IS NOT NULL are used to check for NULL values, and functions like COALESCE assist in managing and substituting NULL in expressions.

Prerequisite:

Before starting to learn the concept of the NULL one only needs proper concentration at first and a very basic knowledge of database commands like Insert, and Create table, and then you are set to go on this journey.

NULL Values

In the world of SQL Server, think of NULL as a special player in the database game—it’s like the blank space in a crossword puzzle waiting to be filled in. Now, imagine your database as a library of tables, each holding different pieces of information. In these tables, every little square, or cell, is like a box eager to be filled with data.

1. Meet the Placeholder

So, here’s where NULL steps in. If you have a box labeled “Date_of_Birth” and you don’t have that info for a certain entry, you don’t just throw in any date. Instead, you use NULL. It’s like you don’t have the information at present to create the entry.

2. Setting NULL Apart

It’s super important to know that NULL isn’t the same as zero, an empty string, or a space. NULL stands out because it means there’s no data—no sneaky placeholder values here. It’s a clear sign that the information is missing or unknown.

3. Missing Info Management

Let’s say you’re keeping track of customer addresses. Some folks haven’t given you their address yet. Instead of guessing and putting in something like “Unknown” or “Not Available,” you use NULL, or if you want to provide a default value, you can use COALESCE or ISNULL to show that, yeah, the data is genuinely missing. COALESCE and ISNULL are explained in this article only so don’t afraid of these clause.

4. Design Flexibility

NULL gives your database design some wiggle room. It lets you make certain columns optional, which is handy when not all entries need the same info. Think of a “Notes” column—NULL can tell you that no notes have been added without resorting to random placeholders.

5. No Ambiguity Zone

Using NULL keeps things crystal clear. When you spot NULL, you know for sure that it’s a deliberate way of saying, “We’re missing info here.” This clarity is gold for doing accurate data analysis and creating reports.

So, in a nutshell, NULL is like the communicator in your database. It shouts, “No info here!” without using confusing placeholders. Embracing NULL as part of your database design is like adding a secret ingredient for more accurate and meaningful data.

Example of SQL Server NULL

Let’s create a simple example to illustrate the use of NULL in SQL Server.

Step 1: Creating a Sample Table

Query

CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
PhoneNumber VARCHAR(15) NULL
);

In this example, the “PhoneNumber” column is set to allow NULL values.

Step 2: Inserting Data

Query:

INSERT INTO Employees (EmployeeID, FirstName, LastName, PhoneNumber)
VALUES
(1, 'John', 'Doe', '123-456-7890'),
(2, 'Jane', 'Smith', NULL),
(3, 'Bob', 'Johnson', '987-654-3210');

Here, the second employee, Jane Smith, doesn’t have a phone number, so we use NULL.

Step 3: Retrieving Data

Query:

SELECT * FROM Employees;

Output:

Employees-Table

OUTPUT

Explanation:

This output displays the data stored in the Employees table. Jane Smith’s entry shows NULL in the PhoneNumber column, reflecting the intentional absence of a phone number for her record. This shows that NULL means simply Nothing to enter hence similarly there is no such info provided and null is displayed as output.

The screenshot visually represents the result of the query execution. The output matches the tabular representation, emphasizing that Jane Smith’s phone number is indeed NULL.

COALESCE and ISNULL Functions

In addition to using NULL, SQL Server provides two functions—COALESCE and ISNULL—to handle situations where NULL values need to be managed or replaced with default values.

1. COALESCE Function

The COALESCE function allows you to return the first non-NULL expression in a list. It comes in handy when you want to provide a fallback value in case a particular column is NULL.

Query:

--**COALESCE FUNCTION**
SELECT COALESCE(PhoneNumber, 'No Phone') AS ModifiedPhoneNumber
FROM Employees;

Output:

Coalsece

Coalesce Function

Explanation: The Output shows that first row with the NULL value got replaced or changed by “No Phone” Value.

2. ISNULL Function

The ISNULL function is similar to COALESCE and replaces NULL with a specified replacement value in the whole table.

-- Using ISNULL
SELECT ISNULL(PhoneNumber, 'No Phone') AS ModifiedPhoneNumber
FROM Employees;

Output:

Coalsece

ISNULL Function

Explanation: Every value in the table with NULL Value got replaced by the “No Phone” Value.

Conclusion

Understanding SQL Server NULL is vital for effective database management. By considering it as an empty placeholder, you can better handle missing or unknown information in your database tables. We have already understood the concept using real-life analogies and cases. This Null concept not only will be helpful in just SQL but also at other places of software engineering.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads