错误 ORA-30076 尝试从 Hana 翻译到 Oracle 时发生

huangapple go评论99阅读模式
英文:

Error ORA-30076 when trying to translate from Hana to Oracle

问题

我试图将一个脚本从Hana转换到Oracle,但在第5行的Rule_def.DATEUPD上一直出现相同的错误,错误消息是该字段不适用于提取。
有人可以告诉我如何在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


我尝试了一些日期公式,但它们不起作用。
英文:

I'm trying to convert a script from Hana to Oracle and I keep getting the same error on Rule_def.DATEUPD on line 5 which says that the field is not valid for the extraction.
Can someone please tell me how to translate that part in Oracle correctly? Thanks!

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

I've tried some date's formulas but they don't work

答案1

得分: 1

您似乎想要使用 `TO_CHAR` 而不是使用 `SUBSTR` 进行复杂格式化:

```lang-sql
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 3 | A

fiddle


<details>
<summary>英文:</summary>

You appear to want to use `TO_CHAR` rather than complex formatting with `SUBSTR`:

```lang-sql
SELECT r.P2_RULE,
       r.P2_ORIGIN,
       r.DATEUPD as P2_DATE,
       TO_CHAR(r.DATEUPD, &#39;HH24:MI:SS&#39;) AS P2_DATETIME,
       r.USERUPD || &#39; | &#39; || 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

Which, for the sample data:

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 &#39;A&#39;, 3 FROM DUAL;

Outputs:

P2_RULE P2_ORIGIN P2_DATE P2_DATETIME P2_USER
1 2 2023-06-05 14:39:03 14:39:03 3 | A

fiddle

答案2

得分: 0

从Oracle文档中,

如果要求HOUR、MINUTE或SECOND,则表达式必须评估为数据类型为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))

以及分钟和秒钟的转换也是如此。

英文:

From the oracle docs,

> If HOUR, MINUTE, or SECOND is requested, then expr must evaluate to an
> expression of data type TIMESTAMP, TIMESTAMP WITH TIME ZONE, TIMESTAMP
> WITH LOCAL TIME ZONE, or INTERVAL DAY TO SECOND. DATE is not valid
> here, because Oracle Database treats it as ANSI DATE data type, which
> has no time fields.

So you need to convert the DATE field's type to TIMESTAMP with:

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

etc. for minute and second also

huangapple
  • 本文由 发表于 2023年6月5日 21:25:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定