我有一个来自mysql的查询,该查询已在最近迁移到DB2的表上运行。
由于下面的行,现在在DB2上查询失败,表示找不到DATEDIFF。我只是因为这不是db2上的有效函数。
在DB2上是否有等效的东西可以保持性能和功能?
SELECT DISTINCT
LEAST(180, DATEDIFF(curdate(), start_date)) as days
FROM table2
where expire_date > curdate()
答案 0 :(得分:2)
我使用DAYS()
函数将日期转换为数字序号,然后将它们相减,如下所示:
SELECT DISTINCT
LEAST(180, DAYS(curdate()) - DAYS(start_date)) as days
FROM table2
where expire_date > curdate()
根据DB2的手册,DAYS()
返回:“结果比0001年1月1日的天数多1。”
答案 1 :(得分:0)
在Db2 11.1(适用于Linux,Unix和Windows)及更高版本上,这将起作用
private static void DropDatabase(IDbConnection connection)
{
var databaseName = GetDatabaseName(connection.ConnectionString);
var str = $@"USE master;
ALTER DATABASE [{databaseName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE [{databaseName}];";
try
{
using (DbCommand command = new SqlConnection(connection.ConnectionString).CreateCommand())
{
command.CommandText = str;
command.CommandType = CommandType.Text;
command.Connection.Open();
command.ExecuteNonQuery();
command.Connection.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
System.Data.SqlClient.SqlException (0x80131904): Cannot open database "MyTestDb_8647C" requested by the login. The login failed.
Login failed for user 'MYDOMAIN\MyServiceUser'.
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at [...].Testing.DatabaseTestBase.DropDatabase(IDbConnection connection) in D:\[...]\DatabaseTestBase.cs:line 37