Understanding the Difference Between MAX() and GREATEST() in SQL Server

SQL Server 2022 introduced the GREATEST() function that returns the maximum value from a list of values. You may be thinking, “but there’s already a MAX() function that returns the maximum value, so why the need for another function that does the same thing?”.

Well here’s the thing – they don’t do the same thing. They’re actually quite different functions, used in different scenarios.

If you’re wondering what the difference is between the MAX() and GREATEST() functions, read on to find out.

Continue reading

Why GENERATE_SERIES() Only Returns the First Value in the Series in SQL Server

If you’re using the GENERATE_SERIES() function to create a series of numbers, but you’re finding that only the first value in the series is returned, it could be something very obvious.

The obvious reason this could happen is that your step value is too big. In particular, if the step is so big that it covers the whole series, then it stands to reason that there will only be one value in the series.

If this is an issue for you, you might want to check that you’re using an appropriate step value. Using a smaller step value can help to create a series with more values.

Continue reading

Improvements to the TRIM(), LTRIM() and RTRIM() Functions in SQL Server 2022

The release of SQL Server 2022 in November 2022 introduced a bunch of new functionality, including some enhancements to the TRIM(), LTRIM() and RTRIM() functions.

The enhancements in the LTRIM() and RTRIM() functions are different to those in the TRIM() function. Below is a quick overview of the enhancements to these functions, along with examples.

Continue reading

Introduction to the GENERATE_SERIES() Function in SQL Server

In SQL Server, the GENERATE_SERIES() function is a relational operator that returns a series of values between a given start and stop point. These are returned in a single-column table.

Although the GENERATE_SERIES() function only works with numeric values, we can combine it with other functions to create a series of dates.

The GENERATE_SERIES() function was introduced in SQL Server 2022 (16.x) and requires the compatibility level to be at least 160.

Continue reading

Understanding the DATE_BUCKET() Function in SQL Server

The release of SQL Server 2022 came with the introduction of the DATE_BUCKET() function.

The DATE_BUCKET() function allows us to arrange data into groups that represent fixed intervals of time. It returns the date/time value that corresponds to the start of each date/time bucket, as defined by the arguments passed to the function.

Continue reading