Categories: SQL Server

Cool Stuff in SQL Server 2022 – IS DISTINCT FROM

I have a blog post series about some nice features in the Snowflake cloud data warehouse; one of them is about the IS [NOT] DISTINCT FROM predicate. I was excited to find out this is now also included in the T-SQL language since the SQL Server 2022 CTP 2.1 preview! You can find the official documentation here.

A quick recap: IS [NOT] DISTINCT FROM allows you to compare two expressions (much like = and <>), but this predicate takes NULL values into account. Basically, itโ€™s a shorter way to write the following:

SELECT *
FROM dbo.FactInternetSales
WHERE OrderDate = ISNULL(@orderdate,'1900-01-01');

Previously, you had to take care of NULL values when working with parameters or nullable columns. Even if you use non-nullable columns, NULL values can slip in when using a LEFT OUTER JOIN for example. So to make sure WHERE clauses or JOINS work like you intended to, you had to do some extra work for those pesky NULL values. But no more! Well, after youโ€™ve upgraded to SQL Server 2022 at least ๐Ÿ™‚

With IS NOT DISTINCT FROM, we can rewrite the SQL like this:

SELECT * FROM dbo.FactInternetSales
WHERE OrderDate IS NOT DISTINCT FROM @orderdate;

Make a habit of writing your WHERE and JOIN clauses with this new predicate (or any Boolean comparison), it can save you some headaches.

You can find more info about this predicate and other new T-SQL stuff in this excellent blog post by Itzik Ben-Gan: Additional T-SQL Improvements in SQL Server 2022. And check out the official announcement of CTP 2.1 for more new features.


------------------------------------------------
Do you like this blog post? You can thank me by buying me a beer ๐Ÿ™‚
Koen Verbeeck

Koen Verbeeck is a Microsoft Business Intelligence consultant at AE, helping clients to get insight in their data. Koen has a comprehensive knowledge of the SQL Server BI stack, with a particular love for Integration Services. He's also a speaker at various conferences.

Recent Posts

Free webinar – Tackling the Gaps and Islands Problem with T-SQL Window Functions

I'm hosting a free webinar at MSSQLTips.com at the 19th of December 2024, 6PM UTC.…

5 days ago

dataMinds Connect 2024 – Session Materials

The slides and scripts for my session "Tackling the Gaps & Islands Problem with T-SQL…

4 weeks ago

Connect to Power BI as a Guest User in another Tenant

Sometimes your Microsoft Entra ID account (formerly known as Azure Active Directory) is added as…

2 months ago

How to use a Script Activity in ADF as a Lookup

In Azure Data Factory (ADF, but also Synapse Pipelines and Fabric Pipelines), you have a…

4 months ago

Database Build Error – Incorrect syntax near DISTINCT

I wrote a piece of SQL that had some new T-SQL syntax in it: IS…

4 months ago

Speaking at dataMinds Connect 2024

I'm very excited to announce I've been selected as a speaker for dataMinds Connect 2024,…

5 months ago