Oracle PL/SQL - Remove Last Instance of a specific Character Oracle PL/SQL - Remove Last Instance of a specific Character json json

Oracle PL/SQL - Remove Last Instance of a specific Character


Its easier to use the rtrim function. So after the loop:

tsResults := rtrim( tsResults, ',' );

Then to terminate the JSON, add the closing curly bracket in the same statement:

tsResults := rtrim( tsResults, ',' ) || '}';


You can use INSTR function to find the position of the last occurance of the "," and then use SUBSTRING.

 SELECT SUBSTR(tsResults , 1, INSTR(tsResults , ',', -1)-1)    INTO tsResults     FROM dual;

If you don't think Regular Expression is an overkill for this then use this statement in your PL/SQL:

 SELECT REGEXP_REPLACE(tsResults , '\s*,\s*$', '')     INTO tsResults     FROM dual;


Tobi, I had a similar scenario, but remember with RTRIM it will remove all instances of the value you have prescribed in the function.

For example:

I would like to trim 'X' from the end of the column selection. (LAST INSTANCE ONLY)

Column Value = 'SOMEVALUEXX'

Now RTrim will return:'SOMEVALUE'

It could be possible that you are after = 'SOMEVALUEX'

Chandu's answer is correct.

Always check your business process.