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

Oracle Adds Support for IF EXISTS and IF NOT EXISTS Syntax Modifiers

Many 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 reading

Fix “An insufficient number of arguments were supplied for the procedure or function GENERATE_SERIES” in SQL Server

If 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.

Continue reading

Fix Error Msg 9810 “The datepart … is not supported by date function datetrunc for data type” in SQL Server

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 reading

DROP TABLE IF EXISTS in SQL

In 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.

Continue reading

Create a Decrementing Series in SQL Server

SQL Server 2022 introduced the GENERATE_SERIES() function that allows us to create a series of values that increment between a given start and end point.

We can create a decrementing series simply by having a greater start point than the end point, while omitting the third argument.

We can also create a decrementing series by using a negative value for the third argument. This obviously requires that the start point is higher than the stop point.

Continue reading

Fix the “Operand type clash” Error when using GENERATE_SERIES() in SQL Server

If you’re getting the “Operand type clash” error when using the GENERATE_SERIES() function in SQL Server, it’s probably because your arguments aren’t of the same type.

The arguments/operands we provide to the GENERATE_SERIES() function/relational operator need to be of the same type. For example, if we pass an integer as the first argument, then the other arguments must also be an integer.

This error may also come with another error which tells us that the input parameters must be of the same type.

To fix, make sure all arguments are of the same type.

Continue reading

Generate Dates Between a Date Range in SQL Server

SQL Server 2022 introduced the GENERATE_SERIES() function, which enables us to create a series of values within a given range. Although this function is limited to just numeric values, we can still combine it with various other functions to create a series of date/time values.

Below are examples of how we can use the GENERATE_SERIES() function to help us get a list of all dates between two given date 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