简体   繁体   English

SQL Server中不存在的Oracle日期时间

[英]Oracle datetime which does not exist in SQL Server

I have a java program where some SQL statements are declared like in this method: 我有一个Java程序,其中声明了一些SQL语句,例如这种方法:

    public ObservableList<TSTTimeZoneConversion> retrieveTimeZoneConversions() throws TSTDBException
{
    String sql = "";
if (DBConnection.getDbType() == DBConnection.DB_ORACLE)
        sql = "SELECT tzc.tzc_id, tzc.tz_id, tzc.offset_to_utc, 
    to_char( tzc.start_time, 'DD-MM-YYYY HH24:MI' ) start_time, 
    to_char( tzc.end_time, 'DD-MM-YYYY HH24:MI' ) end_time, tz.tz_code 
    FROM timezone_conversion tzc, timezone tz WHERE tzc.tz_id = tz.tz_id ORDER BY tzc_id";
else
        sql = "SELECT STATEMENT FOR SQL SERVER";
    return TSTDB.retrieveDB(sql , TSTDB::TSTTimeZoneConversionMapper);
}

My problem is that for example this DD-MM-YYYY HH24:MI does not exist in SQL Server and I could not figure out how to define a date with time. 我的问题是,例如,该DD-MM-YYYY HH24:MI在SQL Server中不存在,并且我无法弄清楚如何用时间定义日期。 I tried it with FORMAT like suggested here in the documentation but I do not know how exactly it works in this case. 我按照文档中此处建议的方式使用FORMAT尝试,但是在这种情况下我不知道它是如何工作的。

My goal is to create a SQL Server query in the else part which is equivalent to the oracle query in the if part. 我的目标是在else部分中创建一个SQL Server查询,该查询等效于if部分中的oracle查询。 I am using SQL Server 2017. 我正在使用SQL Server 2017。

I couldn't find a format mask which matches what you want, to use with SQL Server's CONVERT function. 我找不到要与SQL Server的CONVERT函数配合使用的格式掩码。 But FORMAT seems to work fine here, assuming your version of SQL Server supports it: 但是,假设您的SQL Server版本支持FORMAT在这里似乎可以正常工作:

SELECT
    tzc.tzc_id,
    tzc.tz_id,
    tzc.offset_to_utc,
    FORMAT(tzc.start_time, 'dd-MM-yyyy HH:mm', 'en-US') start_time,
    FORMAT(tzc.end_time, 'dd-MM-yyyy HH:mm', 'en-US') end_time,
    tz.tz_code 
FROM timezone_conversion tzc
INNER JOIN timezone tz
    ON tzc.tz_id = tz.tz_id
ORDER BY
    tzc_id;

Note: The uppercase HH represents hours on the 24 hour clock. 注意:大写字母HH代表24小时制的小时数。 For hours on the 12 hour clock, use lowercase hh . 对于12小时制的小时,请使用小写hh

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM