我们可以将 strright hive 函数转换为 oracle 函数吗

can we convert the strright hive function into oracle functionality

我的项目中有 Hive 代码,我需要将其转换为 Oracle。几乎我已经完成了 oracle 代码,但在 select 语句中面临此 STRRIGHT 功能的问题。

SELECT
clmn1,
clmn2,
CASE
WHEN strright(id,3) like "%d%d%d" THEN strright(id,3)
ELSE id
END

FROM table;

我知道我们可以用 SUBSTR(id,-3) 得到最右边的 3 个字符,但我不确定如何比较最后三个字符是数字还是字符。 通过上面的案例陈述,我可以理解,如果 ID 的最后 3 个字符作为数字,那么它应该只显示这 3 个数字,否则它应该 return 整个 ID。你能帮我解决这个问题吗?我们如何用 sql(oracle) 实现这个输出。

所以 - 如果最后三个字符是数字,那么 return 这三个数字,否则 return 整个 ID?

像这样:

with
  test_data (id) as (
    select 'ab'     from dual union all
    select '23910'  from dual union all
    select 'ab-3'   from dual union all
    select '31-309' from dual union all
    select '33'     from dual
  )
select id, case when regexp_like(id, '\d{3}$')
                then substr(id, -3) else id end as new_str
from   test_data
;

ID      NEW_STR
------  -------
ab      ab    
23910   910   
ab-3    ab-3  
31-309  309   
33      33 

在 Oracle 中很简单:

SELECT
clmn1,
clmn2,
CASE
WHEN REGEXP_LIKE(substr(id,-3), '^[[:digit:]]+$') THEN substr(id,-3)
ELSE id
END
FROM table;