SQL Server ROLLUP() vs WITH ROLLUP

When using the GROUP BY clause in SQL Server, we can use the ROLLUP modifier to create subtotals and grand totals, etc.

You may have seen two variations of this. One as GROUP BY ROLLUP () and the other as GROUP BY ... WITH ROLLUP.

You may be wondering which one you should use?

As it turns out, Microsoft recommends that we use the first syntax; GROUP BY ROLLUP (). The other syntax is provided for backward compatibility only.

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

How to Fix Error 245 When Using LEAST() or GREATEST() in SQL Server

If you’re getting error 245 that reads something like “Conversion failed when converting the varchar value ‘Five’ to data type int” when using the LEAST() or GREATEST() functions in SQL Server, it’s probably because your arguments aren’t of a comparable data type.

When using LEAST() and GREATEST(), all arguments must be of a data type that is comparable and that can be implicitly converted to the data type of the argument with the highest precedence.

To fix this issue, be sure to pass arguments of comparable data types.

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

Fix SQL Server Error 189: “The greatest function requires 1 to 254 arguments”

If you’re getting SQL Server error number 189 that reads “The greatest function requires 1 to 254 arguments“, it’s probably because you’re either passing too few or too many arguments.

As the error message alludes to, you need to pass at least 1 argument, and no more than 254 arguments when using the GREATEST() function.

To fix this issue, be sure to pass at least 1 argument, and no more than 254 arguments.

Continue reading

Fixing the Error: “The function ‘LAST_VALUE’ must have an OVER clause with ORDER BY” in SQL Server

If you’re getting an error message that reads “The function ‘LAST_VALUE’ must have an OVER clause with ORDER BY.” when using the LAST_VALUE() function in SQL Server, it’s probably because you’ve omitted the ORDER BY clause from the OVER clause.

The LAST_VALUE() function requires an OVER clause that contains an ORDER BY clause. This error happens when we include the OVER clause but not the ORDER BY clause.

To fix this error, add an ORDER BY clause to the OVER clause.

Continue reading

Understanding the TRIM() Function in SQL Server

In SQL Server, we can use the TRIM() function to remove leading and trailing characters from a given string.

A common use case is to remove whitespace from the start and end of the string, but we can also specify other characters to remove.

Also, as from SQL Server 2022, we can specify which side of the string to remove the characters from (i.e. leading, trailing, or both).

Continue reading