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:
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.
SQL Server NULL Values
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.
Contact Us