Does anyone know why these:
SELECT RTRIM('123R_CLUSTER', '_CLUSTER') -- should give '123R'
FROM DUAL;
SELECT RTRIM('123S_CLUSTER', '_CLUSTER') -- should give '123S'
FROM DUAL;
SELECT RTRIM('123T_CLUSTER', '_CLUSTER') -- should give '123T'
FROM DUAL;
SELECT RTRIM('123U_CLUSTER', '_CLUSTER') -- should give '123U'
FROM DUAL;
return '123' instead of the expected?
I'm on Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production.
The fun begins when you try these:
The documentation is quite clear:
The Oracle/PLSQL RTRIM function removes all specified characters from the right-hand side of a string.
So it doesn't remove the string _CLUSTER
at the end of your string - it removes characters until there is one that isn't _, C, L, U, S, T, E or R. Since your postfixes are R/S/T/U, they also match the rtrim
condition, and are removed. As would 123S_SLURTE
, for example.
As an easier to understand example,
rtrim('LK_123aababaabbbababbaa', 'ab') // returns LK_123
rtrim
simply isn't the tool for the job at hand :)
And in case you want to know how to easily do this with, try this:
SELECT regexp_substr('123R_CLUSTER', '^(.*)_CLUSTER$', 1, 1, null, 1) from dual
union all
SELECT regexp_substr('123S_CLUSTER', '^(.*)_CLUSTER$', 1, 1, null, 1) from dual
union all
SELECT regexp_substr('123T_CLUSTER', '^(.*)_CLUSTER$', 1, 1, null, 1) from dual
union all
SELECT regexp_substr('123U_CLUSTER', '^(.*)_CLUSTER$', 1, 1, null, 1) from dual;
yields
REGEXP_SUBST
------------
123R
123S
123T
123U
And to decipher the function
regexp_substr(
'123T_CLUSTER', -- source_field
'^(.*)_CLUSTER$', -- regular expression
-- to capture all characters from start of
-- data up to "_CLUSTER"
1, -- start looking at position 1 of string
1, -- which occurance to return
null, -- used for match behaviour
1) -- return what is in first set of parentheses
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.