Oracle SQL add new column based on value of other column - sql

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

Related

Dropping column with quotation marks in column name

I added a column using following commands (I used quotation marks so that N is in upper case in my column name)
ALTER TABLE new_table
ADD “Name” VARCHAR(50);
However, I see "nam" column in my table now after running that command.
How can I drop that column?
ALTER TABLE new_table
DROP COLUMN "Name";
I get following error:
ERROR: column "name" of relation "new_table" does not exist
The following statement causes this error:
ALTER TABLE new_table
DROP COLUMN "Name";
seems you are not using double quote " in first query bus some others quotes try suing the same chars “”
ALTER TABLE new_table DROP COLUMN “Name”;
Check how Postgres is storing the column name when you are using a double quote in column name:
select table_name, column_name from information_schema.columns where
table_name='tab1';
Also, you may view the same when you execute a select statement:
select * from table;
Copy the same column name text in your alter statement. Here is the sample column name with both kind of double quotes in PostgreSQL:
create table tab1(data varchar(30));
alter table tab1 ADD “Name1” varchar(50);
alter table tab1 ADD "Name2" varchar(50);
select *from tab1;
select table_name, column_name from information_schema.columns where table_name='tab1';
alter table tab1 drop column “Name1”;
alter table tab1 drop column "Name2";
table_name column_name
tab1 data
tab1 “name1”
tab1 Name2
Here is the link to the fiddle
Note: Avoid using a double quote in the table name, column, etc. In case you use you have to ensure that the same names are specified in all queries.
Edit:
It's simple. If you use "Name" (not same as “Name”, notice quote angle) in the column name then you have to refer the column as "Name". In case “Name” is used then you have to refer by “Name”. The quotes need to match.
Another observation is when "Name" used as column name it makes the column name as Name (N uppercase) as opposed to all lowercase column names by default in the database but needs to be referred as "Name".

How to replace duplicate words in a column with just one word in SQL Server

I have a few million strings that relate to file paths in my database;
due to a third party program these paths have become nested like below:
C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\
I want update the entries so that thirdparty\thirdparty\etc becomes \thirdparty.
I have tried this code:
UPDATE table
SET Field = REPLACE(Field, 'tables\thirdparty\%thirdparty\%\', 'tables\thirdparty\')
WHILE EXISTS (SELECT * FROM table WHERE Field LIKE '%\thirdparty\thirdparty\%')
BEGIN
UPDATE table SET Field = REPLACE(Field, '\thirdparty\thirdparty\', '\thirdparty\')
END
So do you want something like this?
SELECT SUBSTRING('tables\thirdparty\%thirdparty\%\',0,CHARINDEX('\','tables\thirdparty\%thirdparty\%\',0)) + '\thirdparty\'
OR
UPDATE table
SET Field = REPLACE(Field, Field, (SELECT SUBSTRING(Field,0,CHARINDEX('\',Field,0)) + '\thirdparty\'))
You can avoid using a loop with the following technique:
Update TABLE
SET Field = left(Field,charindex('*',replace(Field, 'thirdparty\', '*'))-1)+'thirdparty\'+right(Field,charindex('*',reverse(replace(Field, 'thirdparty\', '*')))-1)
Its too late, but I just guess if I want replace a single word in repeating multiple time same word as he want. This will replace all with append a single time '\thirdparty'...
Check this.
Declare #table table(Field varchar(max))
insert into #table values('C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\')
,('C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\1')
,('C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\2')
,('C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\3')
,('C:\files\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\thirdparty\unique_bit_here\4')
UPDATE #table
SET Field = SUBSTRING (Field, 1, CHARINDEX('\thirdparty', Field ) ) + 'thirdparty\'
--replace (Field , 'thirdparty\' ,'')
+ reverse( SUBSTRING ( REVERSE(Field), 1, CHARINDEX(reverse('\thirdparty'), REVERSE(Field) )-2 ) )
--REPLACE(Field, 'tables\thirdparty\%thirdparty\%\', 'tables\thirdparty\')
select * from #table

How to replace a value in SQLdata table

I have a table name Image there is two column with Name & Url all the rows in url having same value and starting in that some values are same i want to update the in one query...
For example...
http://farm3.staticflickr.com/2854/10380193164_9b65e4c5ed_n.jpg
I want to replace this and want to add a folder name like pages after the .com/
http://farm3.staticflickr.com/pages/2854/10380193164_9b65e4c5ed_n.jpg
How to do this ?
Please try using REPLACE:
declare #var nvarchar(max)='http://farm3.staticflickr.com/2854/10380193164_9b65e4c5ed_n.jpg'
select REPLACE(#var, '.com/', '.com/pages/')
i.e. for selecting from a table, try
SELECT
REPLACE(ColumnName, '.com/', '.com/pages/') as ColumnName
FROM YourTable
To UPDATE table, use query:
UPDATE YourTable
SET ColumnName=REPLACE(ColumnName, '.com/', '.com/pages/')
#skgacharya, I hope the query for update was already given by #Techdo. Try this...
UPDATE YourTable_name
SET yourURLColumn_Name=REPLACE(yourURLColumn_Name, '.com/', '.com/pages/')

How to replace specific values in a oracle database column?

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.

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