From b982bf54b575dc0accb8ec7bb7f6b0e5d49e579c Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 3 May 2024 10:23:38 +0200 Subject: [PATCH] fix addition of fractional second duration on SQL Server Signed-off-by: Gavin King --- .../org/hibernate/dialect/SQLServerDialect.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerDialect.java index fd68ecde23..a2682c018f 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/SQLServerDialect.java @@ -788,10 +788,15 @@ public class SQLServerDialect extends AbstractTransactSQLDialect { return 6; //microseconds! } + /** + * Even though SQL Server only supports 1/10th microsecond precision, + * we use nanosecond as the "native" precision for datetime arithmetic + * since it simplifies calculations. + */ @Override public long getFractionalSecondPrecisionInNanos() { // return 100; // 1/10th microsecond - return 1; // Even though SQL Server only supports 1/10th microsecond precision, use nanosecond scale for easier computation + return 1; } @Override @@ -823,12 +828,14 @@ public class SQLServerDialect extends AbstractTransactSQLDialect { // there's no dateadd_big()) so here we need to use two // calls to dateadd() to add a whole duration switch (unit) { - case NANOSECOND: + case NANOSECOND: //use nanosecond as the "native" precision case NATIVE: return "dateadd(nanosecond,?2%1000000000,dateadd(second,?2/1000000000,?3))"; // case NATIVE: -// // 1/10th microsecond is the "native" precision +// // we could in principle use 1/10th microsecond as the "native" precision // return "dateadd(nanosecond,?2%10000000,dateadd(second,?2/10000000,?3))"; + case SECOND: + return "dateadd(nanosecond,cast(?2*1e9 as bigint)%1000000000,dateadd(second,?2,?3))"; default: return "dateadd(?1,?2,?3)"; }