SQL DATEDIFF coercion differences between databases on same SQL instance? SQL DATEDIFF coercion differences between databases on same SQL instance? database database

SQL DATEDIFF coercion differences between databases on same SQL instance?


https://dba.stackexchange.com/questions/96101/sql-datediff-coercion-differences-between-databases-on-same-sql-instance

As answered by Aaron Bertrand on the DBA stack exchange site, this was rooted in compatibility levels.

SELECT compatibility_levelFROM sys.databases WHERE name = 'FirstDatabase'90

VS

SELECT compatibility_levelFROM sys.databases WHERE name = 'SecondDatabase'110

Aaron has an excellent write up of why in this question:https://dba.stackexchange.com/questions/44908/what-is-the-actual-behavior-of-compatibility-level-80

See Conversions involving new date/time types

The higher compatibility level likely means DateDiff uses a DateTime2 or other 'wider' data type and works. At 90 or below, it probably uses the old DateTime, and thus has conversion errors.

Thanks to Tab Alleman for the suggestion to cross post.