If you’re using SQL Server’s GENERATE_SERIES()
function/relational operator and you’re getting an empty result set, then it could be due to one of the following reasons.
Category: Relational
CREATE TABLE IF NOT EXISTS Equivalent in SQL Server
In SQL, we can use the CREATE TABLE IF NOT EXISTS
statement to create a table only if it doesn’t exist. The benefit of doing this is that we won’t get an error if there’s already a table with the same name.
But SQL Server doesn’t support this syntax – at least not in the current version of SQL Server at the time of writing (SQL Server 2022) .
So with SQL Server, we need to do a bit of extra work.
Continue readingFix “Argument value 0 is invalid for argument 3 of generate_series function” in SQL Server
If you’re getting SQL Server error 4199 that reads “Argument value 0 is invalid for argument 3 of generate_series function“, it’s probably because you’re passing zero as the third argument to the GENERATE_SERIES()
function.
The GENERATE_SERIES()
function accepts an optional third argument, but this argument can’t be zero.
To fix this error, either pass a non-zero expression as the third argument, or omit the argument altogether (in order to use the default step of 1).
Continue readingGenerate a Time Series in SQL Server
SQL Server’s GENERATE_SERIES()
function returns a series of numbers within a given interval. But just because it returns numbers, doesn’t mean we can’t leverage its functionality in order to generate a series of time values.
If you need to create a series of time values with evenly spaced intervals between a start and end point, maybe the following technique can help.
Continue readingIntroduction to the JSON_PATH_EXISTS() Function in SQL Server
From SQL Server 2022 we can use the JSON_PATH_EXISTS()
function to test whether a specified SQL/JSON path exists in the input JSON string.
It’s similar to the JSON_CONTAINS_PATH()
function that’s available in MySQL and MariaDB.
Fix “The ORDER BY in WITHIN GROUP clause of ‘APPROX_PERCENTILE_CONT’ function must have exactly one expression” in SQL Server
If you’re getting SQL Server error 10751 that reads “The ORDER BY in WITHIN GROUP clause of ‘APPROX_PERCENTILE_CONT’ function must have exactly one expression” it’s probably because you’re using too many ORDER BY
expressions with the APPROX_PERCENTILE_CONT()
function.
The APPROX_PERCENTILE_CONT()
function requires the WITHIN GROUP
clause, and that clause requires an ORDER BY
sub-clause. However, that ORDER BY
sub-clause requires exactly one expression – no more, no less. So, you can’t pass multiple expressions, and you can’t pass zero expressions. It must be exactly one expression.
To fix this issue, be sure to have just one ORDER BY
expression in the WITHIN GROUP
clause when using the APPROX_PERCENTILE_CONT()
function.
Understanding the JSON_ARRAY() Function in SQL Server
In SQL Server, we can use the JSON_ARRAY()
function to construct JSON array text from zero or more expressions.
The resulting array contains the values we provide as arguments. Providing zero expressions results in an empty array.
Continue readingFixing the Error: “The function ‘LAG’ must have an OVER clause with ORDER BY” in SQL Server
If you’re getting error message 4112 that reads “The function ‘LAG’ must have an OVER clause with ORDER BY” in SQL Server, it’s probably because you’re omitting the ORDER BY
clause from the OVER
clause when using the LAG()
function.
The LAG()
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.
An Introduction to the JSON_OBJECT() Function in SQL Server
In SQL Server, we can use the JSON_OBJECT()
function to construct JSON object text from zero or more expressions.
The resulting object contains the key/value pairs that we provide as arguments. Providing zero expressions results in an empty object.
Continue readingWhy 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