Published on

July 30, 2022

New Features in SQL Server 2022

SQL Server 2022 is set to be released later this year, and it brings a host of exciting changes to the database platform. In this article, we will explore some of the new T-SQL features that will be available in SQL Server 2022.

DISTINCT FROM

One of the interesting additions to T-SQL in SQL Server 2022 is the DISTINCT FROM predicate. This function allows you to compare the equality of two expressions, even when there are NULL values involved. This can be useful in scenarios where you want to determine if two values are equal or not, regardless of the presence of NULLs.

For example, let’s say you have a code snippet like this:

DECLARE @a INT, @b INT
SELECT @a = 1, @b = 1
IF @a = @b
 SELECT 'equal', @a, @b
ELSE 
 SELECT 'unequal', @a, @b

In this case, the result will be ‘equal’ because both @a and @b have the same value. However, if @b is NULL, the result will be ‘unequal’ because NULL is not equal to any value. With the DISTINCT FROM predicate, you can simplify this code and get a more accurate result:

DECLARE @a INT, @b INT
SELECT @a = 1, @b = NULL
IF @a IS DISTINCT FROM @b
 SELECT 'unequal', @a, @b
ELSE 
 SELECT 'equal', @a, @b

Now, even though @b is NULL, the result will be ‘unequal’ because the DISTINCT FROM predicate guarantees a true or false result.

DATE_BUCKET

Another useful addition in SQL Server 2022 is the DATE_BUCKET function. This function allows you to determine the starting date of a period based on a specified window or bucket size. It can be particularly helpful when you need to group or calculate periods in your queries.

For example, if you want to divide the year into 4-week buckets, you can use the DATE_BUCKET function to get the starting date of each bucket. This can be done with a simple code snippet:

DECLARE @origin DATE = '2022/01/01';
DECLARE @bucketsize INT = 4
SELECT 
 date_bucket(week, @bucketsize, CAST('2022/01/15' as date), @origin),
 date_bucket(week, @bucketsize, CAST('2022/01/30' as date), @origin),
 date_bucket(week, @bucketsize, CAST('2022/02/25' as date), @origin),
 date_bucket(week, @bucketsize, CAST('2022/03/04' as date), @origin)

The result will be the starting date of each 4-week bucket:

2022-01-01
2022-01-29
2022-02-26
2022-03-05

This can be a powerful tool for analyzing data based on specific time periods.

GENERATE_SERIES

If you are familiar with tally tables, you’ll be pleased to know that SQL Server 2022 introduces the GENERATE_SERIES function, which serves a similar purpose. This function allows you to generate a table of numbers that can be used in various scenarios.

The syntax for GENERATE_SERIES is straightforward:

GENERATE_SERIES(start, stop [, step])

For example, if you want to generate a list of numbers from 1 to 5, you can use the following code:

SELECT * FROM GENERATE_SERIES(1, 5)

What’s interesting is that you can also use decimal values with GENERATE_SERIES. This can be useful when working with percentages or other decimal-based calculations.

GREATEST/LEAST

SQL Server 2022 introduces the GREATEST and LEAST functions, which simplify finding the maximum or minimum value among a set of parameters. These functions are especially useful when dealing with numeric or string values.

For example, if you have a set of numeric values and you want to find the greatest and smallest values, you can use the GREATEST and LEAST functions:

SELECT GREATEST(10, 5, 8, 3) AS MaxValue,
       LEAST(10, 5, 8, 3) AS MinValue

The result will be:

MaxValue | MinValue
---------|---------
   10    |    3

These functions also handle NULL values gracefully, ignoring them in the comparison.

STRING_SPLIT

The STRING_SPLIT function, introduced in previous versions of SQL Server, now includes an optional third parameter that allows you to retrieve the ordinal position of each split value. This ensures that the order of the split values is maintained.

For example, if you want to split a string and retrieve the ordinal position of each value, you can use the following code:

SELECT value, ordinal
FROM STRING_SPLIT('apple,banana,orange', ',', 1)
ORDER BY ordinal

The result will be:

value  | ordinal
-------|--------
apple  |   1
banana |   2
orange |   3

Make sure to include the ORDER BY clause to maintain the correct order of the split values.

DATETRUNC

The DATETRUNC function allows you to truncate a datetime value to a specified part of the date, such as year, month, or hour. This can be useful when you only need to consider the start of a specific period in your calculations.

For example, if you want to remove the day from a date and get the year and month with the day set to 01, you can use the DATETRUNC function:

SELECT DATETRUNC(month, '2022-08-15') AS TruncatedDate

The result will be:

TruncatedDate
-------------
2022-08-01

Keep in mind that DATETRUNC does not support timezone offsets.

Conclusion

These are just a few of the exciting T-SQL features coming in SQL Server 2022. The DISTINCT FROM and GREATEST/LEAST functions provide more flexibility and simplicity in your code, while the DATE_BUCKET function helps with period calculations. The GENERATE_SERIES function is a handy replacement for tally tables, and the enhanced STRING_SPLIT function ensures the correct ordering of split values. Finally, the DATETRUNC function allows for precise truncation of datetime values.

As SQL Server 2022 becomes available, I encourage you to explore these new features and consider how they can improve your SQL Server development and reporting tasks.

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.