From ae63c5de764c6e01c1f875ae03a8c04f8b6f22c4 Mon Sep 17 00:00:00 2001 From: Ed Burn <9583964+edward-burn@users.noreply.github.com> Date: Sat, 6 Dec 2025 12:45:59 +0000 Subject: [PATCH] use DATEDIFF_BIG on sql server --- NEWS.md | 1 + R/backend-mssql.R | 8 ++++---- tests/testthat/test-backend-mssql.R | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/NEWS.md b/NEWS.md index 6e91fe5ff..db13e1ca6 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). * SQL Server: `if`/`ifelse()`, and `if_else()` now use `CASE WHEN` instead of `IIF`. This ensures the handling of `NULL`s matches the R's `NA` handling rules (#1569). * `if_else()` uses simpler translation for `missing` (#1573). * New translations for stringr function `str_ilike()` for Postgres, Redshift, and Snowflake (@edward-burn, #1628). diff --git a/R/backend-mssql.R b/R/backend-mssql.R index 93d15a60f..96a78d12e 100644 --- a/R/backend-mssql.R +++ b/R/backend-mssql.R @@ -414,15 +414,15 @@ simulate_mssql <- function(version = "15.0") { check_dots_empty() check_unsupported_arg(precision, allowed = "day") check_unsupported_arg(n, allowed = 1L) - - sql_expr(DATEDIFF(DAY, !!start, !!end)) + # https://learn.microsoft.com/en-us/sql/t-sql/functions/datediff-big-transact-sql?view=sql-server-ver17 + sql_expr(DATEDIFF_BIG(DAY, !!start, !!end)) }, difftime = function(time1, time2, tz, units = "days") { check_unsupported_arg(tz) check_unsupported_arg(units, allowed = "days") - sql_expr(DATEDIFF(DAY, !!time2, !!time1)) + sql_expr(DATEDIFF_BIG(DAY, !!time2, !!time1)) } ) @@ -723,7 +723,7 @@ utils::globalVariables(c( "DATE", "DATEADD", "DATEFROMPARTS", - "DATEDIFF", + "DATEDIFF_BIG", "DATENAME", "DATEPART", "IIF", 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(