How to replace specific values in a oracle database column? - sql

I am looking to replace values in a particular column. For example the following column values
column name
----------
Test1
Test2
Test3
Test12
should be (replacing est1 with rest1)
column name
----------
Trest1
Test2
Test3
Trest12

Use REPLACE:
SELECT REPLACE(t.column, 'est1', 'rest1')
FROM MY_TABLE t
If you want to update the values in the table, use:
UPDATE MY_TABLE t
SET column = REPLACE(t.column, 'est1', 'rest1')

If you need to update the value in a particular table:
UPDATE TABLE-NAME SET COLUMN-NAME = REPLACE(TABLE-NAME.COLUMN-NAME, 'STRING-TO-REPLACE', 'REPLACEMENT-STRING');
where
TABLE-NAME - The name of the table being updated
COLUMN-NAME - The name of the column being updated
STRING-TO-REPLACE - The value to replace
REPLACEMENT-STRING - The replacement

In Oracle, there is the concept of schema name, so try using this
update schemname.tablename t
set t.columnname = replace(t.columnname, t.oldvalue, t.newvalue);

I'm using Version 4.0.2.15 with Build 15.21
For me I needed this:
UPDATE table_name SET column_name = REPLACE(column_name,"search str","replace str");
Putting t.column_name in the first argument of replace did not work.

Related

Multiple conditional updates in a single sql query PLSQL

I have some values like this in the database with three records
id
TEST_TEST1
TEST_TEST2
TEST_TEST3
Now i need to append all the values with a "PREFIX". So it becomes PREFIX_TEST_TEST1, PREFIX_TEST_TEST2 etc. But for the third value TEST_TEST3, I have to change it to PREFIX_TESTTEST3 (no underscore)
So i made it using a two update queries like below
update table set id=concat('PREFIX',id) where id in ('TEST_TEST1','TEST_TEST2');
and the second update statement
update table set id='PREFIX_TESTTEST3' where id='TEST_TEST3'
Is there any way we can make both these updates in one update statement?
CASE expression helps.
SQL> update test set
id = 'PREFIX_' || case when id = 'TEST_TEST3' then replace(id, '_')
else id
end
where id in ('TEST_TEST1','TEST_TEST2','TEST_TEST3');
3 rows updated.
SQL> select * From test;
ID
------------------------------
PREFIX_TEST_TEST1
PREFIX_TEST_TEST2
PREFIX_TESTTEST3
SQL>
You can use a case expression, for example:
update table
set id = case
when id in ('TEST_TEST1','TEST_TEST2' ) then concat('PREFIX',id)
when id ='TEST_TEST3' then 'PREFIX_TESTTEST3'
end
where id in ('TEST_TEST1','TEST_TEST2','TEST_TEST3')
You can also decode function to do that
update Your_table
set id = 'PREFIX_' || decode( id, 'TEST_TEST3', replace(id, '_', ''), id )
where id in ('TEST_TEST1', 'TEST_TEST2', 'TEST_TEST3')
;

Oracle SQL add new column based on value of other column

I'm looking for some help to create a new column based on values from another column - if this is even possible... This is not an ideal solution but I'm out running out of options.
I need to replace the beginning folder paths, change the direction of the \ and change the extension
Existing Field:
\\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca
New Field:
/location/TELEDATA/2020/Aug/03/5249013/5249013-07-25-18-96572.wav
Oracle version Version 19.2.1.247
Thank you in advance
You can add a new column to your table named NewField:
Alter table TableName add NewField varchar(500);
Then update NewField by replacing some characters as you wish from ExistingField.
update TableName set NewField= replace(replace(existingfield,'\','/'),'.cca','.wav')
Here I have just replace '' with '/' and '.cca' with '.wav'.
To replace path also:
update TableName set NewField= '/location/TELEDATA/'||substr(replace(replace(existingfield,'\','/'),'.cca','.wav'),instr(replace(replace(existingfield,'\','/'),'.cca','.wav'),'/2020',1,1) + 1)
DB-Fiddle:
Schema and insert statements:
create table mytable (existingfield varchar(500));
insert into mytable values('
\\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca');
Add new column:
Alter table mytable add NewField varchar(500);
Update query:
update mytable set NewField= '/location/TELEDATA/'||substr(replace(replace(existingfield,'\','/'),'.cca','.wav'),instr(replace(replace(existingfield,'\','/'),'.cca','.wav'),'/2020',1,1) + 1)
Select query:
select * from mytable;
Output:
EXISTINGFIELD
NEWFIELD
\BRRNAKCL12\Audiofiles22\1Year\Diogarfngal_ZZZZZZZZZ\2020\Aug\03\5249013\5249013-07-25-18-96572.cca
/location/TELEDATA/2020/Aug/03/5249013/5249013-07-25-18-96572.wav
db<>fiddle here

Update table to add a literal value to a column

I am trying to update/replace column values in the table with hard coded value.
value = "c:\temp\"
This:
COLUMN
file.txt
file1.txt
Should become this:
FINAL COLUMN
c:\temp\file.txt
c:\temp\file1.txt
Attempted solution:
SELECT REPLACE(t.column, t.column, 'c:\temp't.column)
FROM TABLE t
Is this correct logic? Do we have another function I can use?
Assuming Oracle:
If you want to change the values in the table permanently you can just run an update query:
update your_table
set your_column = 'C:\temp\' || your_column;
Sample SQL Fiddle
If you're using MS SQL you can do concatenation like this:
MS SQL (all versions?):
update your_table
set your_column = 'C:\temp\' + your_column;
MS SQL 2012 and later:
update your_table
set your_column = concat('C:\temp\',your_column);
Do a UPDATE statement like below in case you want to change it permanently
update table1 set [column] = 'c:\temp\' + [column];
Else, if you just want to display it that way then SELECT query should be
select 'c:\temp\' + [column] as new_col
from table1
NOTE: above code syntax is for SQL Server. Not sure since you tagged as tsql

Any SQL strip function available

I am trying to run an update query which will strip - from a field. For example:
ID NAME
------------
1 you-me
On updating, I want to strip the "-". The result should be:
ID NAME
------------
1 youme
Is there any sql command to do this? I am using DB2.
A straight REPLACE function then?
update tbl set col = REPLACE(col, '-', '')
You can just use SQL REPLACE function
UPDATE table
SET column = REPLACE(column, '-', '');
See this documentation

sql delete from rows value

The database includes a table with a rows:
html/css/java/php
How with sql query delete "java"?
The result would be:
html/css/php
See http://www.w3schools.com/sql/sql_delete.asp for details on DELETE.
DELETE FROM TableName WHERE yourColumn = 'java'
try this (SQL Server syntax, question does not specify which database):
DECLARE #YourTable table (YourColumn varchar(500))
INSERT INTO #YourTable VALUES ('html/css/java/php')
INSERT INTO #YourTable VALUES ('html/css/java')
INSERT INTO #YourTable VALUES ('java/php')
INSERT INTO #YourTable VALUES ('java')
INSERT INTO #YourTable VALUES ('html/css/php')
UPDATE #YourTable
SET YourColumn=REPLACE(
REPLACE(
REPLACE(YourColumn,'/java','')
,'java/',''
)
,'java',''
)
select * from #YourTable
OUTPUT
YourColumn
-------------------
html/css/php
html/css
php
html/css/php
(5 row(s) affected)
I'm not entirely sure what you're asking, because your question isn't very clear.
If "html", "css", "java", and "php" are different values for the same column, this is what you want:
DELETE FROM table_name WHERE column_name = 'java';
You'll need to replace "table_name" with the name of your table and "column_name" with the name of your column.
If there are 4 rows then Aioobe's answer would hold good. If you want to update the column but leave out Java then you should use:
UPDATE table_name SET column_name = Replace(column_name,'java/','')
IF you want to retrieve the information leaving out that data then use:
SELECT Replace(column_name,'java/','') column_name FROM table_name
HTH
I'm not sure what you want to achieve... If you need to delete column 'java', it should look like
ALTER TABLE table1 DROP COLUMN java
Here's another guess:
SQL> select * from t23
2 /
WHATEVER
----------------------------------------------------------------
html/css/java/php
SQL> update t23
2 set whatever = replace(whatever, '/java', null)
3 where whatever like '%/java%'
4 /
1 row updated.
SQL> select * from t23
2 /
WHATEVER
----------------------------------------------------------------
html/css/php
SQL>
There are other ways to do the same thing. For instance some flavours of database support using RegEx in a similar fashion