-1

I want to replace all the occurrences of 4 with the number 2 in a string column of a table. This is a hardcoded value of 2 which replaces all occurrences of the number 4 in a Oracle table. The LOBS column is a VARCHAR column

ID LOBS
1 1,4,6,7,8
2 1,5,6,7,9,4
3 3,5,7,8,11,4

New Table

ID LOBS
1 1,2,6,7,8
2 1,5,6,7,9,2
3 3,5,7,8,11,2
4
  • Which is your DBMS product? Oracle or Mysql? BTW you need a simple REPLACE function. Commented Mar 29, 2022 at 15:34
  • Oracle Sql Developer Commented Mar 29, 2022 at 15:35
  • 1
    Does this answer your question? How to replace specific values in a oracle database column? Commented Mar 29, 2022 at 15:37
  • 1
    @IshaanKanwar Oracle SQL Developer is a client application that connects to many databases; it is NOT a database. Please edit your question to add the correct tag for the database that you are connecting to through SQL Developer. Commented Mar 29, 2022 at 15:46

3 Answers 3

2

In Oracle, you can use simple (quick) string functions:

SELECT id,
       TRIM(BOTH ',' FROM REPLACE(','||lobs||',', ',4,', ',2,'))
         AS updated_lobs
FROM   table_name;

Or (slower) regular expressions:

SELECT id,
       REGEXP_REPLACE(lobs,'(^|,)4(,|$)','\12\2') AS updated_lobs
FROM   table_name

Which, for the sample data:

CREATE TABLE table_name (ID, LOBS) AS
SELECT 1, '1,4,64,7,8' FROM DUAL UNION ALL
SELECT 2, '4,1,5,64,7,9' FROM DUAL UNION ALL
SELECT 3, '3,5,64,8,11,4' FROM DUAL;

Both output:

ID UPDATED_LOBS
1 1,2,64,7,8
2 2,1,5,64,7,9
3 3,5,64,8,11,2

db<>fiddle here

Sign up to request clarification or add additional context in comments.

Comments

0

It seems a simple REPLACE function Oracle will work for you -

SELECT REPLACE(LOBS, '4', '2')
  FROM your_table;

2 Comments

Would this also change 14 to 12?
This does not work as it would replace 14->12
0

You could use REGEP_REPLACE

CREATE TABLE table1 (
  ID INTEGER,
  LOBS VARCHAR2(12)
);
INSERT INTO table1
  (ID, LOBS)
VALUES
  ('1', '1,2,6,7,8');
INSERT INTO table1
  (ID, LOBS)
VALUES
  ('2', '2,6,7,8,9');
  INSERT INTO table1
  (ID, LOBS)
VALUES
  ('3', '1,5,6,7,9,2');
  INSERT INTO table1
  (ID, LOBS)
VALUES
  ('4', '3,5,7,8,11,2');
SELECT REGEXP_REPLACE(
       REGEXP_REPLACE(
       REGEXP_REPLACE(LOBS,'^2,','4,')
       ,'(,2,)',',4,'),',2$',',4') FROM table1
       WHERE 1= 1
| REGEXP_REPLACE(REGEXP_REPLACE(REGEXP_REPLACE(LOBS,'^2,','4,'),'(,2,)',',4,'),',2$',',4') |
| :--------------------------------------------------------------------------------------- |
| 1,4,6,7,8                                                                                |
| 4,6,7,8,9                                                                                |
| 1,5,6,7,9,4                                                                              |
| 3,5,7,8,11,4                                                                             |

db<>fiddle here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.