SQL Server 2022 introduces a new predicate, IS [NOT] DISTINCT FROM, which simplifies the comparison of nullable columns. This feature is a boon for developers who often struggle with the nuanced behavior of NULL values in SQL comparisons. In this blog, we’ll explore how this new predicate works, its benefits, and provide a detailed business use case to illustrate its practical application.
Business Use Case: Analyzing Customer Orders
Imagine a retail company, JB Retail, that maintains a database (JBDB) to track customer orders. The company wants to analyze orders to identify customers who have updated their email addresses. However, due to some data migration issues, there are instances where old and new email addresses might be stored as NULL values.
To accurately identify customers who have changed their email addresses (or those whose email addresses are currently NULL but were previously not NULL), the IS [NOT] DISTINCT FROM predicate becomes very useful. This new feature allows us to simplify the logic and handle NULL comparisons more gracefully.
Setting Up the JBDB Database
First, let’s create the JBDB database and set up a sample table CustomerOrders to illustrate our use case.
-- Create JBDB database
CREATE DATABASE JBDB;
GO
-- Use the JBDB database
USE JBDB;
GO
-- Create CustomerOrders table
CREATE TABLE CustomerOrders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OldEmail NVARCHAR(255),
NewEmail NVARCHAR(255),
OrderDate DATE
);
GO
-- Insert sample data into CustomerOrders
INSERT INTO CustomerOrders (OrderID, CustomerID, OldEmail, NewEmail, OrderDate)
VALUES
(1, 101, 'old_email1@example.com', 'new_email1@example.com', '2024-01-15'),
(2, 102, 'old_email2@example.com', NULL, '2024-02-20'),
(3, 103, NULL, 'new_email3@example.com', '2024-03-05'),
(4, 104, 'old_email4@example.com', 'old_email4@example.com', '2024-04-10'),
(5, 105, NULL, NULL, '2024-05-12');
GO
Understanding IS [NOT] DISTINCT FROM Predicate 🧩
The IS DISTINCT FROM predicate compares two expressions and returns TRUE if they are distinct (i.e., not equal or one is NULL and the other is not). The IS NOT DISTINCT FROM predicate, on the other hand, returns TRUE if they are not distinct (i.e., equal or both are NULL).
This is particularly useful when dealing with nullable columns, as NULL values are traditionally not equal to anything, including themselves, in SQL. The new predicate addresses this challenge.
Example Queries
Finding Customers Who Have Updated Their Email Address
SELECT CustomerID, OldEmail, NewEmail
FROM CustomerOrders
WHERE OldEmail IS DISTINCT FROM NewEmail;
This query identifies customers whose email addresses have changed. The IS DISTINCT FROM predicate ensures that it catches cases where either the old or new email could be NULL.
Finding Customers Whose Email Address Remains Unchanged
SELECT CustomerID, OldEmail, NewEmail
FROM CustomerOrders
WHERE OldEmail IS NOT DISTINCT FROM NewEmail;
This query retrieves customers whose email addresses have not changed, including cases where both old and new emails are NULL.
Detailed Business Use Case 🎯
Let’s dive deeper into how JB Retail can use these queries to improve their customer relationship management. The company plans to send personalized emails to customers whose email addresses have been updated, acknowledging the change and ensuring it was intentional.
Business Workflow
Identify Updated Emails: The company will first use the IS DISTINCT FROM query to extract a list of customers with updated emails.
SELECT CustomerID, OldEmail, NewEmail
FROM CustomerOrders
WHERE OldEmail IS DISTINCT FROM NewEmail;
- This query helps them identify cases where:
- The old email was
NULLand the new email is not, indicating a new addition. - The new email was
NULLand the old email is not, indicating a removal. - Both emails are different but not
NULL, indicating an actual change.
- The old email was
- Personalized Communication: Once the list is prepared, JB Retail can use it to send personalized communication to these customers. This step ensures that customers are aware of the changes and can report if the change was not authorized.
- Customer Service Follow-up: For cases where both old and new emails are
NULL, the company can follow up with these customers to update their contact information, ensuring they do not miss out on important communications.
Find Customers with NULL Values in Either Old or New Email
This query helps identify customers where either the old or new email address is NULL, but not both.
SELECT CustomerID, OldEmail, NewEmail
FROM CustomerOrders
WHERE OldEmail IS DISTINCT FROM NewEmail
AND (OldEmail IS NULL OR NewEmail IS NULL);
List Orders with Same Email Address Before and After
This query lists orders where the email address remained the same before and after, but takes NULL into account.
SELECT OrderID, CustomerID, OldEmail, NewEmail
FROM CustomerOrders
WHERE OldEmail IS NOT DISTINCT FROM NewEmail
AND (OldEmail IS NOT NULL AND NewEmail IS NOT NULL);
Find Orders with NULL Emails in Both Old and New
This query identifies orders where both the old and new email addresses are NULL.
SELECT OrderID, CustomerID, OldEmail, NewEmail
FROM CustomerOrders
WHERE OldEmail IS NOT DISTINCT FROM NewEmail
AND OldEmail IS NULL;
Identify Changes Where Old Email is NULL and New Email is Not
This query finds orders where the old email address was NULL and the new email address is not NULL.
SELECT OrderID, CustomerID, OldEmail, NewEmail
FROM CustomerOrders
WHERE OldEmail IS DISTINCT FROM NewEmail
AND OldEmail IS NULL
AND NewEmail IS NOT NULL;
Find Orders Where Both Emails are Different or Both are NULL
This query lists orders where the old and new emails are either both different or both NULL.
SELECT OrderID, CustomerID, OldEmail, NewEmail
FROM CustomerOrders
WHERE (OldEmail IS DISTINCT FROM NewEmail
AND OldEmail IS NOT NULL AND NewEmail IS NOT NULL)
OR (OldEmail IS NULL AND NewEmail IS NULL);
These queries leverage the IS [NOT] DISTINCT FROM predicate to handle various scenarios involving NULL values, providing flexibility and clarity in managing data comparisons. Feel free to adapt these queries based on your specific needs!
Conclusion 🏁
The introduction of the IS [NOT] DISTINCT FROM predicate in SQL Server 2022 is a significant enhancement for database developers and administrators. It simplifies the handling of NULL values in comparisons, making queries more readable and efficient.
In the case of JB Retail, this feature enables a more accurate and efficient way to handle email updates, ensuring that the company maintains accurate customer contact information and strengthens its customer relationship management processes.
With these new tools at your disposal, handling NULL values in SQL Server has never been easier! 🎉
For more tutorials and tips on SQL Server, including performance tuning and database management, be sure to check out our JBSWiki YouTube channel.
Thank You,
Vivek Janakiraman
Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided “AS IS” with no warranties, and confers no rights.
- IS DISTINCT FROM
- IS NOT DISTINCT FROM
- SQL NULL handling
- sql server 2022
- SQL Server 2022 blog
- SQL Server 2022 enhancements
- SQL Server 2022 features
- SQL Server 2022 IS DISTINCT FROM
- SQL Server 2022 IS NOT DISTINCT FROM
- SQL Server 2022 updates
- SQL Server advanced predicates
- SQL Server advanced queries
- SQL Server analysis tools
- SQL Server Best Practices
- SQL Server best practices 2022
- SQL Server blog
- SQL Server blog posts
- SQL Server business analysis
- SQL Server business scenarios
- SQL Server business use cases
- SQL Server comparison functions
- SQL Server comparison operators
- SQL Server customer data
- SQL Server customer orders
- SQL Server data accuracy
- SQL Server data analysis
- SQL Server data handling
- SQL Server Data Integrity
- SQL Server Data Management
- SQL Server data processing
- SQL Server data updates
- SQL Server database features
- SQL Server Database Management
- SQL Server database management tools
- SQL Server database operations
- SQL Server database queries
- SQL Server database use cases
- SQL Server Development
- SQL Server efficient data management.
- SQL Server efficient queries
- SQL Server email address queries
- SQL Server email analysis
- SQL Server email change analysis
- SQL Server email comparison
- SQL Server email updates
- SQL Server feature highlights
- SQL Server features
- SQL Server Functions
- SQL Server handling business data
- SQL Server handling data
- SQL Server handling NULL
- SQL Server new features
- SQL Server new predicate
- SQL Server NULL comparison
- SQL Server NULL values
- SQL Server nullable columns
- SQL Server Performance
- SQL Server practical examples
- SQL Server practical query examples
- SQL Server predicates
- SQL Server Queries
- SQL Server query demonstration
- SQL Server query efficiency
- SQL Server query examples
- SQL Server query handling
- SQL Server query handling tips
- SQL Server query insights
- SQL Server Query Optimization
- SQL Server query optimization tips
- SQL Server query results
- SQL Server query results analysis
- SQL Server query techniques
- SQL Server query use cases
- SQL Server SQL Server 2022 features
- SQL Server SQL Server development
- SQL Server Tips
- SQL Server tips and tricks
- SQL Server Tutorials
- SQL Server tutorials and guides
- SQL Server Updates