错误ORA-30076当尝试从Hana转换到Oracle

mpgws1up  于 2023-06-22  发布在  Oracle
关注(0)|答案(2)|浏览(222)

我试图将一个脚本从Hana转换到Oracle,但我在Rule_def.DATEUPD的第5行上一直得到相同的错误,它说该字段对于提取无效。有人能告诉我如何在Oracle中正确翻译这部分吗?谢谢!

SELECT 
    P2_RULE,
    P2_ORIGIN,
    Rule_def.DATEUPD as P2_DATE,
    substr('0' || extract(hour from Rule_def.DATEUPD),2) 
    || ':' || 
    substr('0' || extract(minute from Rule_def.DATEUPD),2)
    || ':' || 
    substr('0' || extract(second from Rule_def.DATEUPD),2) AS P2_DATETIME,
    Rule_def.USERUPD || ' | ' || DESC_UTENTE as P2_USER
FROM (
    SELECT 
        P2_RULE,
        P2_ORIGIN,
        DATEUPD,
        USERUPD,
        ROW_NUMBER() over (partition by P2_RULE, p2_origin order by DATEUPD desc) as N_row
    FROM AW_PII_P20018_000001
    WHERE dateupd is not null) Rule_def
join UTENTE
ON Rule_def.USERUPD = COD_UTENTE    
WHERE N_ROW = 1

我试过一些日期的公式,但他们不工作

hgtggwj0

hgtggwj01#

您似乎希望使用TO_CHAR,而不是使用SUBSTR进行复杂的格式化:

SELECT r.P2_RULE,
       r.P2_ORIGIN,
       r.DATEUPD as P2_DATE,
       TO_CHAR(r.DATEUPD, 'HH24:MI:SS') AS P2_DATETIME,
       r.USERUPD || ' | ' || u.DESC_UTENTE as P2_USER
FROM   ( SELECT P2_RULE,
                P2_ORIGIN,
                DATEUPD,
                USERUPD,
                ROW_NUMBER() over (
                  partition by P2_RULE, p2_origin order by DATEUPD desc
                ) as N_row
         FROM   AW_PII_P20018_000001
         WHERE dateupd is not null
       ) r
       join UTENTE u
       ON r.USERUPD = u.COD_UTENTE    
WHERE  r.N_ROW = 1

其中,对于样本数据:

CREATE TABLE AW_PII_P20018_000001 (P2_RULE, P2_ORIGIN, DATEUPD, USERUPD) AS
SELECT 1, 2, SYSDATE, 3 FROM DUAL;

CREATE TABLE utente (desc_utente, cod_utente) AS
SELECT 'A', 3 FROM DUAL;

输出:
| P2_RULE| P2_ORIGIN| P2_DATE| P2_DATETIME| P2_USER|
| - -----|- -----|- -----|- -----|- -----|
| 1| 2| 2023-06-05 14:39:03| 14点39分03秒||3A|
fiddle

lfapxunr

lfapxunr2#

从甲骨文文档中,
如果请求HOUR、MINUTE或SECOND,则expr必须计算为数据类型为TIMESTAMP、TIMESTAMP WITH TIME ZONE、TIMESTAMP WITH LOCAL TIME ZONE或INTERVAL DAY TO SECOND的表达式。DATE在此处无效,因为Oracle数据库将其视为ANSI DATE数据类型,该数据类型没有时间字段。
因此,您需要使用以下命令将DATE字段的类型转换为TIMESTAMP:

extract(hour from cast(Rule_def.DATEUPD as timestamp))

等也为分和秒

相关问题