如何在Oracle SQL中删除帐户列的前3个和后5个字符?

g6ll5ycj  于 12个月前  发布在  Oracle
关注(0)|答案(4)|浏览(196)

如何在Oracle SQL中删除帐户列的前3个和后5个字符?
我有一个列帐户。示例帐号“99k9220000709999”结果应显示帐号为“92200007”。需要Oracle SQL查询。

8zzbczxx

8zzbczxx1#

substr应该可以做到这一点-从位置4开始,取列的长度减去5+3=8:

SELECT SUBSTR(mycolumn, 4, LENGTH(mycolumn) - 8)
FROM   mytable

字符串

biswetbf

biswetbf2#

另一种方法是左修剪部分,直到字母结束的字符,然后提取剩下的字符串,不包括最后五个字符,

WITH t(str) AS
(
 SELECT '99k9220000709999' FROM DUAL
), t2 AS
(
SELECT regexp_substr(str,'[^[:alpha:]]+$') as str
  FROM t
)
SELECT substr( str,1, length(str)-5 ) as "Result"
  FROM t2

字符串

esbemjvw

esbemjvw3#

如果我们能把它复杂化(为了好玩),为什么要把它简单化呢?

SQL> with test (id, account) as
  2    (select 1, '99k9220000709999' from dual union all
  3     select 2, '002LF004828xx'    from dual
  4    ),
  5  split as
  6    (select id,
  7            column_value lvl,
  8            regexp_substr(account, '[^.]', 1, column_value) chr,
  9            max(length(account)) over (order by null) len
 10     from test cross join table(cast(multiset(select level from dual
 11                                              connect by level <= length(account)
 12                                             ) as sys.odcinumberlist))
 13    )
 14  select id,
 15         listagg(chr, '') within group (order by lvl) result
 16  from split
 17  where lvl between 4 and len - 5
 18  group by id;

        ID RESULT
---------- --------------------
         1 92200007
         2 LF004828

SQL>

字符串

wz3gfoph

wz3gfoph4#

你可以使用substr()length()。另一种方法是使用正则表达式:

select regexp_replace('99k9220000709999', '^.{3}(.*).{5}$', '\1')
from dual;

字符串
但这不一定是最有效的方法。

相关问题