How to update an already existing value in oracle sql column - sql

I tried to find possible solution on my question, but without success. Let say that I have a TEST table with many records and one of the columns is called CA_GROUP. That column contain the following values:
{"TEST1":"1","TEST2":"2"}
I want to add this part ',"TEST3":"3"' to the already existing values in that column. So the result should be as:
{"TEST1":"1","TEST2":"2","TEST3":"3"}
The only thing that I know is this:
update test t
set t.ca_group = replace(t.ca:group, '{"TEST1":"1","TEST2":"2"}'
, '{"TEST1":"1","TEST2":"2","TEST3":"3"}')
where id = xxxxxx
and other conditions.
update test t
set t.ca_group = replace (t.ca:group, '{"CODE1":"1","CODE2":"2"}'
, '{"CODE1":"1","CODE2":"2","TEST3":"3"}')
where id = xxxxxx
and other conditions.
But this is not efficient for me, because I have a lot of records and I need to add the same value in all columns one by one. Is there any smartest way of doing this ?

How about appending 'TEST3' to each existing value?
update test t
set t.ca_group = substr(t.ca_group, 1, length(t.ca_group) - 1) || ',"TEST3":"3"}'
where id = xxxxxx

Related

UPDATE and REPLACE part of a existing column value with new column value

I am trying to update a existing column value with new value in sql table.
For example, My table has below data
table1:
ID
RequestName
1
Victor-123
2
Hello-123
3
Victor-124
4
Victor-125
5
Hi-123
6
Victor-126
In the above table I want to update Request Name column value wherever we have Victor, I want to replace with Victor-ID. For example
for ID 1 we have RequestName column value is Victor-123. I want to update it with Victor-ID-123 using Sql. I know we can do it with update sql statement, but if we have lot of data how to achieve that or replace 'Victor' with 'Victor-ID'. Since we might have different values in Request Name column. I want to update only column value with Victor to Victor-ID in table
Any help, I appreciate it
Thank you
UPDATE [tablename] SET [RequestName] = REPLACE(RequestName,'Victor','Victor-ID')
Here are two examples:
Example 1:
update table1
set RequestName = REPLACE(RequestName, 'Victor', 'Victor-ID')
where RequestName like 'Victor-%'
Example 2 (this one will let you use this for any name by changing the where clause):
update table1
set RequestName = LEFT(RequestName, CHARINDEX('-', RequestName) - 1) + '-ID-' + RIGHT(RequestName, LEN(RequestName) - CHARINDEX('-', RequestName))
where RequestName like 'Victor-%'
I don't believe example 1 actually requires the where clause though, as it should only affect those with "Victor". However, if you have something like "Victor1-123" that should not be changed, the where clause will prevent that.
Edit: Just occurred to me that example 1 can be adjusted to:
update table1
set RequestName = REPLACE(RequestName, 'Victor-', 'Victor-ID')
This will allow you to eliminate the where clause.

This Oracle SQL doesn't work in Oracle Apex

if :p6_internal_id is null then
INSERT INTO table
(
id,
account
)
values
(
tseq_id.nextval,
:p6_account,
);
else
update table set "all columns" where id = :p6_internal_id;
end if ;
This says
ORA-00927: missing equal sign
for the update set "all columns" line.
I don't know how to fix this. How do I set the value of all the columns where id is what I enter?
Based on your comments, you just need to change that line to:
update table set account = :p6_account where id = :p6_internal_id;
You don't need to set the id column to :p6_internal_id as you know it already has that value - since you're using it in the where clause.
There is no magic value of '"all columns"' that would allow every column to be updated at once, not least because you need to supply a value that corresponds to every column anyway, and in the right order.
If you have multiple columns to set then you have to list them all explicitly, with each column/value pair separated by commas; e.g. with a few made-up columns and bind variables:
update table set account = :p6_account,
name = :p6_name,
amount := p6_amount
where id = :p6_internal_id;
You can see the required syntax in the documentation.

SQL update set table if value in table A is equals to value in table B

this query is working fine.
UPDATE data
SET unit_id='a3110a89'
WHERE unit_id='7d18289f';
Now, I need to run this query over 30 times
so I made csv file import it to the DB with this command:
COPY mytable FROM 'D:/test.csv' WITH CSV HEADER DELIMITER AS ','
Now I have table called my table with 2 columns OLD and NEW
i want to search the table "data" in column unit_id anywhere there if the value equals to the value in table "mytable.old" replace it with the value "mytable.new" on the same row.
I tried to run this query but I get an error:
UPDATE data
SET unit_id=(SELECT mytable."old" FROM public.mytable)
WHERE unit_id=(SELECT mytable."new" FROM public.mytable)
error:
more than one row returned by a subquery used as an expression
I think i'm just trying to do it in the wrong way...
thx for the help!
by the way Im using PostgreSQL
Your subqueries need to be correlated to the outer update:
UPDATE data
SET unit_id = (SELECT mytable."new" FROM public.mytable where data.old = mytable.old)
WHERE unit_id in (SELECT mytable."old" FROM public.mytable);
That is, set the unit_id to the "new" value, when you find the "old" value in the table.
Can you try like this,
UPDATE data A
SET A.unit_id=B.old
FROM (SELECT mytable."old",mytable."new" FROM public.mytable) B
WHERE A.unit_id=B.new
UPDATE data A
SET unit_id = B."old"
FROM public.mytable B
WHERE A.unit_id = B."new"
;
BTW: it looks like you also have old and new swapped in your question. Do you really want A's value to be set to B's old field?

CONCAT in Oracle

I've been researching the concat function extensively but hit a wall creating a temp table. I have two columns: ID (ex. 4323) and Source (ex. PHI). I want to add a column that includes a prefix of "API-" to the ID column (ex. API-4332). Anyone have any insight?
All rows?
UPDATE TheTable
SET id = 'API-'||id;
Or,
UPDATE TheTable
SET id = CONCAT('API-',id);
EDIT:
Your problem statement led me to believe you wanted to create a new column ("I want to add a column..."). Sorry for the confusion. I have changed the answer to update the ID column.

Convert value in all rows to VARBINARY(50)

I want to upgrade all columns in a table, what I am aiming to do is retreive a column from the row it will update then update it, something like:
update works
set encrpyted_item_no = (CAST(RTrim((
select unencrypted_item_no
from works
where name = name
) AS VARBINARY(50))
I know that query is wrong, it's just an example to show you what I am aiming to do.
I want it to select the column unencrypted_item_no from its row then update that same row with the data it gets from unencrypted_item_no, doing this for the whole table.
How would I accomplish this?
You shouldn't need to do a sub-select, referencing the other column in the set will work on a row by row basis, ie:
UPDATE works
SET encrpyted_item_no = CAST(RTrim(unencrypted_item_no) AS varbinary(50))
Shouldn't this be enough ?
update works
set encrypted_item_no = CAST(RTrim(unencrypted_item_no) AS VARBINARY(50))