How to replace a comma separated value in table column with user input value oracle - sql

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

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).

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

Related

How to find all entries that match part of a string criteria but not another in Oracle SQL

I have a column like:
Values
111.111.111-Dummy
111.111.111-Dummy2
111.111.111-X
222.222.222-Dummy
222.222.222-Dummy2
333.333.333-Dummy
333.333.333-Dummy2
333.333.333-X
I need to find the numbers that do not have an entry with "-X" in the end.
So in this scenario the query should show: 222.222.222.
My idea so far was to first trim the results to only have the numbers part (or everything before the '-')
But I don't know what to do next. How can I find entries that don't match in the same column and same table?
select substr(values_, 1, instr(values_, '-') - 1) as numbers
from {your-table}
group by substr(values_, 1, instr(values_, '-') - 1)
having count(case when values_ like '%-X' then 1 end) = 0;
values is a reserved keyword in Oracle, and therefore it can't be an identifier (such as a column name); I changed it by adding a trailing underscore.
Note that this assumes all "values" are followed by a dash and a (possibly empty) string. If you may also have values like 111.11.1111 (with no dash at the end) then the query must be modified slightly, but I assumed there aren't any - otherwise you should have included one or two in your sample.
Use not like in a having clause:
select substring_index(values, '-', 1)
from t
group by substring_index(values, '-', 1)
having sum(values like '%-x') = 0;

Filter centered column SQL Oracle

I try to create a query from an Oracle DB.
that is, SELECT FROM and WHERE.
the column "ORG" is centered and always has 4 letters. I would like to filter that on one specific Item/ value.
I already have WHERE ORG = 'HHAH'
or with SBSTRG (ORG ...:
somehow nothing works.
Does somebody has any idea?
I have values of ' HHAH ' instead of 'HHAH' in the column. There are blanks befor and after the value
You could remove the leading and trailing spaces with the trim() function:
WHERE TRIM(ORG) = 'HHAH'
Using a function on the column value will prevent any index on that column being used (as will like with a leading wildcard); unless you add a function-based index for the trimmed value there isn't much you can do about that.
Do you need the LIKE operator:
WHERE ORG LIKE '%HHAH%'
or
WHERE ORG LIKE '%' || 'HHAH' || '%'
to search for values conatining 'HHAH'?
I would recommend fixing the data:
update t
set org = trim(org);
I see no reason to be storing spaces in the name of an org. If you need spaces for reporting purposes, put them there.

SQlite query to update column and replace a value

I want to update a column that contains a string like 12,43,433 I want to only replace 43 with another number say 54 so that the column value becomes 12,54,433.
How can I do that??
You can use REPLACE() function like this:
UPDATE YourTable a
SET a.StringColumn = REPLACE(a.StringColumn,',43,',',54,')
WHERE a.StringColumn like '%,43,%'
Storing lists as strings is a very bad idea. SQL has a great data structure for storing lists. It is called a table, not a string. The proper way to store lists is to use a junction table.
Sometimes we are stuck with other people's really bad design decisions. If so, you can do:
update t
set col = trim(replace(',' || col || ',', ',43,', ',54,'), ',')
where ',' || col || ',' like '%,43,%';
Notes:
This works regardless of where the "43" appears in the string (including at the beginning and end).
This maintains the format of the string with no commas at the beginning and end.
This only attempts to update rows that have the particular elements in the list.
Use of such a query should really be a stopgap while you figure out how to fix the data structure.

Using SQL to make specific changes in a database.

I am trying to figure out some commands/code in SQL.
I have database with names, addresses IDs etc, but I have to convert firstname values ending in “jnr” to “(Jnr)” and those ending in “snr” to “(Snr)”.
How do I do this?
update table TABLE_NAME set NAMES = '*xyz*Jnr' where NAMES like '%jnr'
Update or select:
PASTE(column, CHAR_LENGTH(column)-3, 1, UPPER(SUBSTRING(column FROM CHAR_LENGTH(column)-3 FOR 1)
WHERE column LIKE '%jnr' OR column LIKE '%snr'
PASTE is used to put in one character at position 3 from end,
CHAR_LENGTH to get length of column value,
UPPER converts character to upper case,
SUBSTRING is used to pick one character here (j or s),
LIKE is used to find values ending with jnr, or snr.
All ANSI SQL (no dbms specified!)

Select rows that has mixed charcters in a single value e.g. 'Joh?n' in name column

In an oracle table:
1- a value in a VARCHAR column contains characters that are not letters.
Consider a scenarion where a name in 'last_name' column contains regular characters (A - Z, a - z) as well as characters that are not english letters (e.g. '.', '-', ' ','_', '>' or similar).
The challenge is to select the rows that has names in 'last_name' as '.John' or 'John.' or '-John' or 'Joh-n'
2- Is it possible to have non-date values in a Date defined column? If yes, how can such records be selected in an oracle query?
Thanks!
I believe this will do the trick:
SELECT * FROM mytable WHERE REGEXP_LIKE(last_name, '[^A-Za-z]');
As for your 2nd question, I am unsure. I would be glad if someone else could add on to what I have to answer your 2nd question. I have found this website thought that might be of help. http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html
It explains the DATE format.
If I properly understand your goal, you need to select rows with last_name column containing the name 'John', but it may also have additional characters before, after, or even inside the name. In that case, this should be helpful:
select * from tab where regexp_replace(last_name, '[^A-Za-z]+', '') = 'John'