Fix “The date value is less than the minimum date value allowed for the data type” When using SQL Server’s DATETRUNC() Function

If you’re getting SQL Server error msg 9837 that tells you “The date value is less than the minimum date value allowed for the data type…”, it sounds like you’re using the DATETRUNC() function with the week date part on a date that would cause the result to backtrack to a date that’s earlier than the date type supports.

This is a rare error that only occurs when using the week date part with the DATETRUNC() function on a very small number of early dates.

One way to deal with this error would be to use iso_week instead of week, if that’s suitable for your particular situation.

Continue reading

Generate 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 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

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

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

How to Convert Time Zones in MySQL using the Time Zone Name

When using a function like CONVERT_TZ() to convert between time zones in MySQL, we provide the date/time value, along with the original time zone and the destination time zone (i.e. the time zone that we’re converting to).

One typical way to do this is to use the time zone offset, such as −05:00 to specify the time zone. Another method is to use the time zone name. However, this requires that we have configured named time zones in MySQL.

Continue reading

Fix Error Msg 237 “There is insufficient result space to convert a money value to int” in SQL Server

If you’re getting error msg 237 that reads There is insufficient result space to convert a money value to int, it’s because you’re trying to convert a money value to an integer but the number’s too big to fit into an integer.

To fix this issue, make sure you convert the value to a data type that can handle the size of the number that you’re trying to convert.

Continue reading