diff --git a/NEWS.md b/NEWS.md index e7acd1146..df98eec28 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # dbplyr (development version) +* SQL Server uses `DATEDIFF_BIG` instead of `DATEDIFF` to work regardless of data size (@edward-burn, #1666). * `na_matches = "na"` now works correctly with inequality and overlap joins, preserving the comparison operator instead of converting to equality (#1505). * `copy_inline()` now works with blob columns (#1515). * `tbl_sql()` is now (soft) deprecated. It hasn't been required in a while. diff --git a/R/backend-mssql.R b/R/backend-mssql.R index eaf93915c..4f572de20 100644 --- a/R/backend-mssql.R +++ b/R/backend-mssql.R @@ -417,15 +417,15 @@ simulate_mssql <- function(version = "15.0") { check_dots_empty() check_unsupported_arg(precision, allowed = "day") check_unsupported_arg(n, allowed = 1L) - - sql_glue("DATEDIFF(DAY, {start}, {end})") + # https://learn.microsoft.com/en-us/sql/t-sql/functions/datediff-big-transact-sql?view=sql-server-ver17 + sql_glue("DATEDIFF_BIG(DAY, {start}, {end})") }, difftime = function(time1, time2, tz, units = "days") { check_unsupported_arg(tz) check_unsupported_arg(units, allowed = "days") - sql_glue("DATEDIFF(DAY, {time2}, {time1})") + sql_glue("DATEDIFF_BIG(DAY, {time2}, {time1})") } ) diff --git a/tests/testthat/test-backend-mssql.R b/tests/testthat/test-backend-mssql.R index f3fc6a3a7..10f399355 100644 --- a/tests/testthat/test-backend-mssql.R +++ b/tests/testthat/test-backend-mssql.R @@ -189,7 +189,7 @@ test_that("custom clock functions translated correctly", { expect_translation( con, date_count_between(date_column_1, date_column_2, "day"), - "DATEDIFF(DAY, `date_column_1`, `date_column_2`)" + "DATEDIFF_BIG(DAY, `date_column_1`, `date_column_2`)" ) expect_snapshot( error = TRUE, @@ -217,12 +217,12 @@ test_that("difftime is translated correctly", { expect_translation( con, difftime(start_date, end_date, units = "days"), - "DATEDIFF(DAY, `end_date`, `start_date`)" + "DATEDIFF_BIG(DAY, `end_date`, `start_date`)" ) expect_translation( con, difftime(start_date, end_date), - "DATEDIFF(DAY, `end_date`, `start_date`)" + "DATEDIFF_BIG(DAY, `end_date`, `start_date`)" ) expect_snapshot(