Datediff Overflow Error in Dockerized SQL Server

Posted by Daniel on Tuesday, April 16, 2024

Recently, I encountered an interesting error while using SQL Server in a Docker container. The error was a Microsoft.Data.SqlClient.SqlException with the message “The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large.”

This error occurred when running an application that utilizes Optimizely and interacts with a SQL Server database running within Docker. The issue arose when a stored procedure, netSchedulerList, attempted to calculate the difference in seconds between two dates: LastPing and the current UTC date (GETUTCDATE()).

Upon examining the stack trace, I discovered that GETUTCDATE() was returning a date from the year 2119. This resulted in an enormous time difference when DATEDIFF(second, LastPing, GETUTCDATE()) was executed, exceeding the limits of what the DATEDIFF function in SQL Server can handle.

Root Causes of the Error

  1. Incorrectly Set Date in Docker Container: The error stemmed from the Docker container’s system clock being incorrectly set, causing GETUTCDATE() to return a date far in the future.

  2. Large Time Difference: Because LastPing was likely a past date and GETUTCDATE() was a date in the year 2119, the difference in seconds was massive, exceeding the capabilities of the DATEDIFF function in SQL Server.

Resolution

To resolve this issue, I opted to restart the entire Docker environment, which reset the system clock and corrected the date problem. Following the restart, SQL Server within Docker provided accurate date values to my application, and the error disappeared.


comments powered by Disqus