How to replace a value in SQLdata table - sql

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/')

Related

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

SQL get all records which contains invalid character for e-diaeresis

I have some values in table which text contains A« instead of ë
How to replace those characters with ë in SQL ?
I tried with
SELECT *
FROM [dbo].[Table]
WHERE [dbo].[Table].CustomValue LIKE '%A«%'
How to update those items in query ?
If you want just to SELECT the data then
SELECT *, REPLACE(CustomValue, 'A«', N'ë')
FROM [dbo].[Table]
WHERE [dbo].[Table].CustomValue LIKE '%A«%'
If you really needs to UPDATE the data then
UPDATE [dbo].[Table]
SET CustomValue = REPLACE(CustomValue, 'A«', N'ë')
WHERE [dbo].[Table].CustomValue LIKE '%A«%'
An UPDATE statment with the REPLACE function should do the trick.
UPDATE [dbo].[Table]
SET CustomValue = REPLACE(CustomValue, 'A«', 'ë')
WHERE CustomValue LIKE '%A«%'
Maybe you are looking just for
update dbo.table
set customvalue = replace(customvalue, 'A«', 'ë')
where customvalue like '%A«%';
But maybe your collation does not allow the character 'ë'. In that case you'd have to change the column definition first. E.g.
alter table dbo.table
alter column customvalue nvarchar(1000) collate latin1_general_cs_as;

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

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

How to replace a string in a SQL Server Table Column

I have a table (SQL Sever) which references paths (UNC or otherwise), but now the path is going to change.
In the path column, I have many records and I need to change just a portion of the path, but not the entire path. And I need to change the same string to the new one, in every record.
How can I do this with a simple update?
It's this easy:
update my_table
set path = replace(path, 'oldstring', 'newstring')
UPDATE [table]
SET [column] = REPLACE([column], '/foo/', '/bar/')
I tried the above but it did not yield the correct result. The following one does:
update table
set path = replace(path, 'oldstring', 'newstring') where path = 'oldstring'
UPDATE CustomReports_Ta
SET vchFilter = REPLACE(CAST(vchFilter AS nvarchar(max)), '\\Ingl-report\Templates', 'C:\Customer_Templates')
where CAST(vchFilter AS nvarchar(max)) LIKE '%\\Ingl-report\Templates%'
Without the CAST function I got an error
Argument data type ntext is invalid for argument 1 of replace function.
You can use this query
update table_name set column_name = replace (column_name , 'oldstring' ,'newstring') where column_name like 'oldstring%'
all answers are great but I just want to give you a good example
select replace('this value from table', 'table', 'table but updated')
this SQL statement will replace the existence of the word "table"
(second parameter) inside the given statement(first parameter) with the third parameter
the initial value is this value from table but after executing replace function it will be this value from table but updated
and here is a real example
UPDATE publication
SET doi = replace(doi, '10.7440/perifrasis', '10.25025/perifrasis')
WHERE doi like '10.7440/perifrasis%'
for example if we have this value
10.7440/perifrasis.2010.1.issue-1
it will become
10.25025/perifrasis.2010.1.issue-1
hope this gives you better visualization
select replace(ImagePath, '~/', '../') as NewImagePath from tblMyTable
where "ImagePath" is my column Name. "NewImagePath" is temporery
column Name insted of "ImagePath" "~/" is my current string.(old
string) "../" is my requried string.(new string)
"tblMyTable" is my table in database.
you need to replace path with the help of replace function.
update table_name set column_name = replace(column_name, 'oldstring', 'newstring')
here column_name refers to that column which you want to change.
Hope it will work.
If target column type is other than varchar/nvarchar like text, we need to cast the column value as string and then convert it as:
update URL_TABLE
set Parameters = REPLACE ( cast(Parameters as varchar(max)), 'india', 'bharat')
where URL_ID='150721_013359670'
You also can replace large text for email template at run time, here is an simple example for that.
DECLARE #xml NVARCHAR(MAX)
SET #xml = CAST((SELECT [column] AS 'td','',
,[StartDate] AS 'td'
FROM [table]
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))
select REPLACE((EmailTemplate), '[#xml]', #xml) as Newtemplate
FROM [dbo].[template] where id = 1