Index: src/org/hibernate/dialect/SQLServerDialect.java
===================================================================
--- src/org/hibernate/dialect/SQLServerDialect.java	(revision 11251)
+++ src/org/hibernate/dialect/SQLServerDialect.java	(working copy)
@@ -34,6 +34,9 @@
 
 		registerFunction( "trim", new AnsiTrimEmulationFunction() );
 
+        registerFunction( "milliseconds_between", new SQLFunctionTemplate(Hibernate.LONG, "datediff(millisecond, ?1, ?2)" ) );
+        registerFunction( "seconds_between", new SQLFunctionTemplate(Hibernate.LONG, "datediff(second, ?1, ?2)" ) );
+
 		registerKeyword( "top" );
 	}
 
Index: src/org/hibernate/dialect/Oracle9Dialect.java
===================================================================
--- src/org/hibernate/dialect/Oracle9Dialect.java	(revision 11251)
+++ src/org/hibernate/dialect/Oracle9Dialect.java	(working copy)
@@ -134,8 +134,11 @@
 		registerFunction( "next_day", new StandardSQLFunction("next_day", Hibernate.DATE) );
 
 		registerFunction( "str", new StandardSQLFunction("to_char", Hibernate.STRING) );
-	}
 
+        registerFunction( "milliseconds_between", new SQLFunctionTemplate(Hibernate.LONG, "TO_NUMBER(SUBSTR((?2 - ?1)*24*60*60,1,10) || '000')" ) );
+        registerFunction( "seconds_between", new SQLFunctionTemplate(Hibernate.LONG, "TO_NUMBER(SUBSTR((?2 - ?1)*24*60*60,1,10))" ) );
+    }
+
 	public String getAddColumnString() {
 		return "add";
 	}
Index: src/org/hibernate/dialect/PostgreSQLDialect.java
===================================================================
--- src/org/hibernate/dialect/PostgreSQLDialect.java	(revision 11251)
+++ src/org/hibernate/dialect/PostgreSQLDialect.java	(working copy)
@@ -92,6 +92,8 @@
 		registerFunction( "now", new NoArgSQLFunction("now", Hibernate.TIMESTAMP) );
 		registerFunction( "timeofday", new NoArgSQLFunction("timeofday", Hibernate.STRING) );
 		registerFunction( "age", new StandardSQLFunction("age") );
+        registerFunction( "milliseconds_between", new SQLFunctionTemplate(Hibernate.LONG, "extract(epoch from (?2 - ?1)) * 1000" ) );
+        registerFunction( "seconds_between", new SQLFunctionTemplate(Hibernate.LONG, "extract(epoch from (?2 - ?1))" ) );
 
 		registerFunction( "current_user", new NoArgSQLFunction("current_user", Hibernate.STRING, false) );
 		registerFunction( "session_user", new NoArgSQLFunction("session_user", Hibernate.STRING, false) );
Index: src/org/hibernate/dialect/DerbyDialect.java
===================================================================
--- src/org/hibernate/dialect/DerbyDialect.java	(revision 11251)
+++ src/org/hibernate/dialect/DerbyDialect.java	(working copy)
@@ -31,8 +31,12 @@
 		super();
 		registerFunction( "concat", new VarArgsSQLFunction( Hibernate.STRING, "(","||",")" ) );
 		registerFunction( "trim", new DerbyTrimFunctionEmulation() );
-	}
 
+        registerFunction( "milliseconds_between", new SQLFunctionTemplate(Hibernate.LONG, "{fn timestampdiff(SQL_TSI_FRAC_SECOND, ?1, ?2)}" ) );
+        registerFunction( "seconds_between", new SQLFunctionTemplate(Hibernate.LONG, "{fn timestampdiff(SQL_TSI_SECOND, ?1, ?2)}" ) );
+
+    }
+
 	/**
 	 * This is different in Cloudscape to DB2.
 	 */
@@ -183,7 +187,7 @@
 					}
 				}
 				else {
-					throw new HibernateException( "cannot specify trim character when using Derby as Derby does not support the ANSI trim function, not does it support a replace function to properly emmulate it" );
+					throw new HibernateException( "cannot specify trim character when using Derby as Derby does not support the ANSI trim function, nor does it support a replace function to properly emmulate it" );
 				}
 			}
 		}
Index: src/org/hibernate/dialect/HSQLDialect.java
===================================================================
--- src/org/hibernate/dialect/HSQLDialect.java	(revision 11251)
+++ src/org/hibernate/dialect/HSQLDialect.java	(working copy)
@@ -16,6 +16,7 @@
 import org.hibernate.dialect.function.NoArgSQLFunction;
 import org.hibernate.dialect.function.StandardSQLFunction;
 import org.hibernate.dialect.function.VarArgsSQLFunction;
+import org.hibernate.dialect.function.SQLFunctionTemplate;
 import org.hibernate.dialect.lock.LockingStrategy;
 import org.hibernate.dialect.lock.SelectLockingStrategy;
 import org.hibernate.exception.JDBCExceptionHelper;
@@ -98,6 +99,8 @@
 		registerFunction( "second", new StandardSQLFunction( "second", Hibernate.INTEGER ) );
 		registerFunction( "dayname", new StandardSQLFunction( "dayname", Hibernate.STRING ) );
 		registerFunction( "monthname", new StandardSQLFunction( "monthname", Hibernate.STRING ) );
+        registerFunction( "milliseconds_between", new SQLFunctionTemplate(Hibernate.LONG, "datediff('millisecond', ?1, ?2)" ) );
+        registerFunction( "seconds_between", new SQLFunctionTemplate(Hibernate.LONG, "datediff('second', ?1, ?2)" ) );
 
 		registerFunction( "abs", new StandardSQLFunction( "abs" ) );
 		registerFunction( "sign", new StandardSQLFunction( "sign", Hibernate.INTEGER ) );
Index: src/org/hibernate/dialect/SybaseDialect.java
===================================================================
--- src/org/hibernate/dialect/SybaseDialect.java	(revision 11251)
+++ src/org/hibernate/dialect/SybaseDialect.java	(working copy)
@@ -66,7 +66,10 @@
 		registerFunction( "year", new StandardSQLFunction("year", Hibernate.INTEGER) );
 		registerFunction( "datename", new StandardSQLFunction("datename", Hibernate.STRING) );
 
-		registerFunction( "abs", new StandardSQLFunction("abs") );
+        registerFunction( "milliseconds_between", new SQLFunctionTemplate(Hibernate.LONG, "datediff(millisecond, ?1, ?2)" ) );
+        registerFunction( "seconds_between", new SQLFunctionTemplate(Hibernate.LONG, "datediff(second, ?1, ?2)" ) );
+
+        registerFunction( "abs", new StandardSQLFunction("abs") );
 		registerFunction( "sign", new StandardSQLFunction("sign", Hibernate.INTEGER) );
 
 		registerFunction( "acos", new StandardSQLFunction("acos", Hibernate.DOUBLE) );
Index: src/org/hibernate/dialect/MySQL5Dialect.java
===================================================================
--- src/org/hibernate/dialect/MySQL5Dialect.java	(revision 11251)
+++ src/org/hibernate/dialect/MySQL5Dialect.java	(working copy)
@@ -2,13 +2,24 @@
 
 import java.sql.Types;
 
+import org.hibernate.Hibernate;
+import org.hibernate.dialect.function.SQLFunctionTemplate;
+
 /**
  * An SQL dialect for MySQL 5.x specific features.
  *
  * @author Steve Ebersole
  */
 public class MySQL5Dialect extends MySQLDialect {
-	protected void registerVarcharTypes() {
+
+    public MySQL5Dialect()
+    {
+        super();
+        registerFunction( "milliseconds_between", new SQLFunctionTemplate(Hibernate.LONG, "timestampdiff(FRAC_SECOND, ?1, ?2) / 1000" ) );
+        registerFunction( "seconds_between", new SQLFunctionTemplate(Hibernate.LONG, "timestampdiff(SECOND, ?1, ?2)" ) );
+    }
+
+    protected void registerVarcharTypes() {
 		registerColumnType( Types.VARCHAR, "longtext" );
 		registerColumnType( Types.VARCHAR, 16777215, "mediumtext" );
 		registerColumnType( Types.VARCHAR, 65535, "varchar($l)" );
Index: src/org/hibernate/dialect/DB2Dialect.java
===================================================================
--- src/org/hibernate/dialect/DB2Dialect.java	(revision 11251)
+++ src/org/hibernate/dialect/DB2Dialect.java	(working copy)
@@ -94,8 +94,9 @@
 		registerFunction("week", new StandardSQLFunction("week", Hibernate.INTEGER) );
 		registerFunction("week_iso", new StandardSQLFunction("week_iso", Hibernate.INTEGER) );
 		registerFunction("year", new StandardSQLFunction("year", Hibernate.INTEGER) );
+        registerFunction("seconds_between", new SQLFunctionTemplate(Hibernate.LONG, "(DAYS(enddate) - DAYS(startdate)) * 86400 + (MIDNIGHT_SECONDS(enddate) - MIDNIGHT_SECONDS(startdate))") );
 
-		registerFunction("double", new StandardSQLFunction("double", Hibernate.DOUBLE) );
+        registerFunction("double", new StandardSQLFunction("double", Hibernate.DOUBLE) );
 		registerFunction("varchar", new StandardSQLFunction("varchar", Hibernate.STRING) );
 		registerFunction("real", new StandardSQLFunction("real", Hibernate.FLOAT) );
 		registerFunction("bigint", new StandardSQLFunction("bigint", Hibernate.LONG) );
