我有一个与此类似的查询:
SELECT YEAR_CODE FROM YEAR_CODES
并且它返回几条记录:通常为1,但有时为2或3。返回的记录如下:2018FOO
,2019BAR
我需要获得返回代码的前一年的匹配记录。例如:
正在寻找类似于:
REGEX_REPLACE(SELECT YEAR_CODE FROM YEAR_CODES, 4th character, 4th character minus 1)
您可以使用字符串操作:
with c as (
<your query here>
)
select
from year_code yc
where to_number(substr(yc.code, 1, 4)) = to_number(substr(c.code)) - 1 and
substr(yc.code, 5) = substr(c.code, 5)
您不需要regexp_replace()
,使用substr()
字符串运算符就足够了:
with year_codes(year_code) as
(
select '2018FOO' from dual union all
select '2019BAR' from dual
)
select concat(substr(year_code,1,4) - 1,substr(year_code,-3)) as year_code
from year_codes;
YEAR_CODE
---------
2017FOO
2018BAR