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

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.

Related

Using REPLACE AND LIKE

I am currently trying to update a column in a temporary table using Oracle 11g SQL syntax. In this column there is an Unique ID that is 12 digits long. However I need to join this table with this column holding the Unique ID but the syntax for the Unique ID of this table is slightly different than the syntax for the table that it will be joined (with Unique ID serving as the PK = FK). This may be tough to follow so I will provide what I am doing below.
UniqueID Column from TABLE xyz Syntax
AB10783421111111
UniqueID Column from TABLE zxo Syntax
383421111111
You see how the numbers are identical except for the AB107 and first '3' in the zxo table? I would like to know why both these queries are not running
UPDATE temp37 SET UNIQUE_ID = REPLACE(UNIQUE_ID, (LIKE 'AB107%'), (LIKE '3%'));
UPDATE temp37
SET UNIQUE_ID = '3%'
WHERE UNIQUE_ID = 'AB107%';
Essentially I would like to replace every case of an id with AB10755555555555 to 355555555555. Thank you for any help.
You can do:
UPDATE temp37 SET UNIQUE_ID = REPLACE(UNIQUE_ID, 'AB107', '3');
OR
UPDATE temp37 SET UNIQUE_ID = CONCAT('3', substr(UNIQUE_ID, 6)) WHERE UNIQUE_ID LIKE 'AB107%';

How to update an already existing value in oracle sql column

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

Duplicate a column into a temporary table then convert data

Is there a way to duplicate a column from a current database table (copy all the column contents from table to a temporary table), Then
Convert the string value in the column and increment it by 1, then
Put all those values in a form of a string back into it's original table?
So pseudocode would look like:
copy column1 from tblReal into tmpcolumn in tblTemp (set tmpcolumn1 as nvarchar(265))
update tblTemp
set tmpcolumn1 = 'TESTDATA' + 1
copy tbmpcolumn1 from tblTemp into column1 in tblReal
So actually you want to change a string column, which holds actually a number, by incrementing its value by 1. Why would you need three steps for that? Just do an update statement on the column immediatly. I don't see why you need intermediate tables.
UPDATE tblReal SET column1 = column1 + 1
Piece of cake. You can use the cast function to transform the varchar to a number and back again in the update statement.

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?

Delete the column for the particular value

Using Sql Server 2005
Table1
ID Name Value
001 Rajesh 90
002 Suresh 100
003 Mahesh 200
004 Virat 400
...
I want to delete the value from the table1 for the particular id
Tried Query
Delete value from table1 where id = '001'
The above query is not working.
How to make a delete query for delete the particular column
Need Query Help
There are at least two errors with your statement:
The word table will give a syntax error because it is a reserved word. You need to specify the table name of the specific table you wish to delete from.
Also you cannot write DELETE value FROM. It's just DELETE FROM. And note that it deletes the entire row, not just a single value.
A correct delete statement would look like this:
DELETE FROM table1
WHERE id = '001'
However if you want to change a single value to NULL you should use an UPDATE statement.
UPDATE table1
SET value = NULL
WHERE id = '001'
Of course this assumes that the column is nullable. If not, you'll have to fix that first. See this question for details:
Altering a column to be nullable
I think you want to set the value to null
update Table1 set value=NULL where id='001'