From ac40e37700f84ca5d630d0e6c91882a5edc958e9 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Thu, 2 May 2024 19:58:23 +0200 Subject: [PATCH] fix for datetime subtraction on DB2 Signed-off-by: Gavin King --- .../java/org/hibernate/dialect/DB2Dialect.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java index b4c6d18628..1fcf0d8f5e 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/DB2Dialect.java @@ -476,7 +476,11 @@ public class DB2Dialect extends Dialect { switch ( unit ) { case NATIVE: case NANOSECOND: - pattern.append( "(seconds_between(" ); + pattern.append( "(seconds_between(date_trunc('second'," ); + pattern.append( toExpression ); + pattern.append( "),date_trunc('second'," ); + pattern.append( fromExpression ); + pattern.append( "))" ); break; //note: DB2 does have weeks_between() case MONTH: @@ -484,14 +488,18 @@ public class DB2Dialect extends Dialect { // the months_between() function results // in a non-integral value, so trunc() it pattern.append( "trunc(months_between(" ); + pattern.append( toExpression ); + pattern.append( ',' ); + pattern.append( fromExpression ); + pattern.append( ')' ); break; default: pattern.append( "?1s_between(" ); + pattern.append( toExpression ); + pattern.append( ',' ); + pattern.append( fromExpression ); + pattern.append( ')' ); } - pattern.append( toExpression ); - pattern.append( ',' ); - pattern.append( fromExpression ); - pattern.append( ')' ); switch ( unit ) { case NATIVE: pattern.append( "+(microsecond(");