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.
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.
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.
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 readingIf 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.
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 readingIf 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 readingMany RDBMSs implement IF EXISTS
and IF NOT EXISTS
syntax modifiers that can be used with DDL object creation, modification, and deletion, such as CREATE TABLE
and DROP TABLE
statements, to name just a couple.
These syntax modifiers allow us to run such statements without getting an error in the event that the object already exists (if we’re trying to create it or modify it) or doesn’t exist (if we’re trying to drop it).
Continue readingIf you’re getting error 313 with a message that reads “An insufficient number of arguments were supplied for the procedure or function GENERATE_SERIES” in SQL Server, it’s because you’re not passing enough arguments to the GENERATE_SERIES()
function.
The GENERATE_SERIES()
function/relational operator accepts a minimum of two arguments, and a maximum of three (at least, this is the case at the time of writing). Passing less than two arguments will result in the above error.
To fix this error, be sure to provide at least two arguments when using the GENERATE_SERIES()
function.
If you’re getting SQL Server error msg 9810 that tells you that the datepart “is not supported by date function datetrunc for data type“, it’s probably because you’re using an invalid datepart
argument when using the DATETRUNC()
function.
For example, this error can occur when using a time date part on a date value (i.e. one that doesn’t have a time component). Conversely, it can also occur when using a date date part on a time value (i.e. one that doesn’t have a date component).
Continue readingIn SQL, we can use the DROP TABLE IF EXISTS
statement to drop a table only if it exists.
While it may seem obvious that we can only drop a table if it exists (i.e. we can’t drop a table that doesn’t exist), there’s a good reason for using this statement.
The reason we put an IF EXISTS
clause into a DROP TABLE
statement is to prevent any errors that would occur if the table doesn’t exist.