Skip to content
sqlwithgabriel
Go back

Converting INT to DATETIME in SQL Server: How Does It Work?

Updated:

You’d expect converting an arbitrary string to DATETIME to fail with a conversion error. But why does it work with INT values? Or rather, why does it only work for a certain range of values?

Table of contents

Open Table of contents

The DATETIME range

According to Microsoft’s documentation for the DATETIME data type, this is its range of values:

DATETIME date range in the Microsoft documentation: January 1, 1753 through December 31, 9999

In other words, a DATETIME column can only store dates between January 1, 1753 and December 31, 9999. Any attempt to set a value outside that range results in an error.

The default value

The documentation also tells us that the default value for DATETIME is January 1, 1900:

DATETIME default value in the Microsoft documentation: 1900-01-01 00:00:00

Note

This does not mean that a DATETIME variable declared without a value automatically gets this date. It will be NULL.

The default value is used when an operation doesn’t specify a date, for example in explicit or implicit conversions.

Converting INT to DATETIME

The information above helps us understand how DATETIME works, but how does it answer the original question?

If we declare an INT variable with the value 0 and convert it to DATETIME, we get this:

Converting 0 to DATETIME returns 1900-01-01 00:00:00.000

SQL Server automatically converted 0 to the default value mentioned above.

Now with the value 1, SQL Server returns January 2, 1900:

Converting 1 to DATETIME returns 1900-01-02 00:00:00.000

What’s happening is that when you convert an integer to DATETIME, SQL Server adds that number as days to the default value 1900-01-01:

The same thing happens with negative integers, except now the days are subtracted. Setting -1 returns 1899-12-31:

Converting -1 to DATETIME returns 1899-12-31 00:00:00.000

The range of valid integers

So integers are added as days to the default value. Does that work for any integer?

As we saw earlier, DATETIME only accepts values between 1753-01-01 and 9999-12-31. So the only integers that can be converted are the ones whose number of days, added to or subtracted from the default value, still lands inside that range.

Knowing that, we can calculate the number of days between 1753-01-01 and the default value, and between the default value and 9999-12-31:

SELECT
    DATEDIFF(DAY, '19000101', '17530101') AS min_int,
    DATEDIFF(DAY, '19000101', '99991231') AS max_int;

The smallest possible value is -53690 and the largest is 2958463:

DATEDIFF results: -53690 and 2958463

Converting those results back to DATETIME returns, as expected, exactly the boundaries of the type:

SELECT
    CONVERT(DATETIME, -53690)  AS lower_bound,
    CONVERT(DATETIME, 2958463) AS upper_bound;

Converting -53690 and 2958463 returns 1753-01-01 and 9999-12-31

IntegerResulting DATETIME
-536901753-01-01 00:00:00.000
-11899-12-31 00:00:00.000
01900-01-01 00:00:00.000
11900-01-02 00:00:00.000
29584639999-12-31 00:00:00.000

Integers out of range

Only values between -53690 and 2958463 can be converted to DATETIME, because they represent the limits of the data type.

This is the error you get when you try to convert an integer outside that range:

SELECT CONVERT(DATETIME, 2958464);
Msg 8115, Level 16, State 2
Arithmetic overflow error converting expression to data type datetime.

Arithmetic overflow error when converting 2958464 to DATETIME

In theory, 2958464 would represent January 1 of the year 10000. But since it goes past the upper limit of DATETIME, the conversion fails with an overflow error. The same happens with -53691 or any other integer outside the range.

Conclusion

The initial questions look simple, but they help us understand how DATETIME works and how integers are converted to dates.

The idea for this post came from a real case: I had to figure out why a conversion error suddenly started happening “out of nowhere” in a production environment.

Knowing how the data types work was one of the things that let me act quickly and pinpoint the problem. This kind of fundamental understanding can be crucial when solving complex issues in critical environments.

References


Share this post:

Next Post
Your Disk Is Probably Provisioned Wrong (And I Can Prove It)