0


I have a table in oracle with a column with comma separated values. What i need is when a user enters a value and if that value is present in any of the rows, it should be removed.
eg.

COL

123,234
56,123

If user enters 123, the 1st column should have only 234 and second row should have only 56.
How do we do this in oracle??
Please help
Thanks

2
  • do you always have a maximumn of two values per row ? btw if you add SQL tag to your question im sure it will help Commented May 17, 2011 at 7:56
  • There might even be single, double ot multiple values in that column and not necessarily two. Commented May 17, 2011 at 9:00

2 Answers 2

3
delete from yourtable t
where
  instr(','||t.col||',', '123') > 0

You can replace '123' with a parameter if you like.

But a better way would be not to store comma separated values, and create a detail table instead. If you need to look for a specific value within a comma separated list, you cannot make use of indices, amongst other limitations.

[edit] Misunderstood the question. You meant this:

update YourTable t
set
  t.col = substr(substr(replace(','||t.col||',', ',123,', ','), 2), -2)
where
  instr(','||t.col||',', '123') > 0
  • Add ',' before and after to match items at the beginning or end of the value.
  • Replace using the value ',123,' (within comma's) to prevent accidentally matching 1234 too.
  • Use substr twice to remove the first and last character (the added commas)
  • Use instr in the where to prevent updating records that don't need to be updated (better performance).
Sign up to request clarification or add additional context in comments.

1 Comment

This would delete the entire record - he only wants to remove the specified value from the comma delimited list.
1

try this :

UPDATE t
SET col = REPLACE(REPLACE(col, '&variable', ''), ',', '') FROM t ;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.