Oracle - update string to replace only last character

I have the following line in an Oracle 9i database:

A, B, C

I need to replace all instances of ',' when this is the last item on the line. I came up with the following statement, but it removes everything in the field, not just a comma. Any suggestions?

UPDATE table SET column = REPLACE(SUBSTR(column, -1, 1), ',', ''); 
+4
source share
4 answers

You forgot to add the condition: WHERE SUBSTR(column, -1, 1) = ','
Quassnoi caught another problem - REPLACE returns null - you cannot use it inside "set"

Full sql:

 UPDATE table SET column = SUBSTR(column, 0, length(column)-1) WHERE SUBSTR(column, -1, 1) = ','; 

This ensures that you only replace lines that have values ​​that end with ","

+3
source

rtrim(column, ',') efficient and significantly shorter

+5
source
 UPDATE mytable SET column = SUBSTR(column, 1, LENGTH(column) - 1) WHERE SUBSTR(column, -1, 1) = ',' 
+1
source

If you want to direct the "column" only 1 time to your query, just follow these steps:

UPDATE table SET column = REVERSE(SUBSTR(REVERSE(column), 2));

0
source

Source: https://habr.com/ru/post/1502871/


All Articles