Jak zastąpić ciąg znaków w kolumnie tabeli SQL Server

Mam tabelę (SQL Sever), która odwołuje się do ścieżek (UNC lub innych), ale teraz ścieżka się zmieni.

W kolumnie path mam wiele rekordów i muszę zmienić tylko część ścieżki, ale nie całą ścieżkę. I muszę zmienić ten sam ciąg na nowy, w każdym rekordzie.

Jak mogę to zrobić za pomocą prostego update?

Author: DineshDB, 2009-05-02

9 answers

It ' s this easy:

update my_table
set path = replace(path, 'oldstring', 'newstring')
 517
Author: cjk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-11-12 19:47:12
UPDATE [table]
SET [column] = REPLACE([column], '/foo/', '/bar/')
 116
Author: Marc Gravell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-05-02 09:47:07

Próbowałem powyższego, ale nie dało to prawidłowego wyniku. Następujący robi:

update table
set path = replace(path, 'oldstring', 'newstring') where path = 'oldstring'
 23
Author: Caesar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-03-02 18:52:13
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%'

Bez Funkcji CAST mam błąd

Argument typ danych ntext jest nieprawidłowy dla argumentu 1 funkcji replace.

 15
Author: Igor Bakay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-12-09 14:51:39

Możesz użyć tego zapytania

update table_name set column_name = replace (column_name , 'oldstring' ,'newstring') where column_name like 'oldstring%'
 6
Author: Nitika Chopra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-27 12:00:28

Wszystkie odpowiedzi są świetne, ale chcę dać ci dobry przykład

select replace('this value from table', 'table',  'table but updated')

To polecenie SQL zastąpi istnienie słowa " table" (drugi parametr) wewnątrz podanego polecenia (pierwszy parametr) z trzecim parametrem

Wartością początkową jest this value from table, ale po wykonaniu funkcji replace będzie to this value from table but updated

A oto prawdziwy przykład

UPDATE publication
SET doi = replace(doi, '10.7440/perifrasis', '10.25025/perifrasis')
WHERE doi like '10.7440/perifrasis%'

Na przykład jeśli mamy tę wartość

10.7440/perifrasis.2010.1.issue-1

Stanie się

10.25025/perifrasis.2010.1.issue-1

Hope this gives you better visualization

 5
Author: Basheer AL-MOMANI,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-24 16:01:20
select replace(ImagePath, '~/', '../') as NewImagePath from tblMyTable 

Gdzie "ImagePath" to nazwa mojej kolumny.
"NewImagePath" jest tymczasowym nazwa kolumny "ImagePath"
"~/" to mój obecny ciąg.(stare string)
"../ "jest moim wymaganym sznurkiem.(new string)
"tblMyTable" to moja tabela w bazie danych.

 4
Author: Durgesh Pandey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-03-30 14:53:48

Jeśli docelowy typ kolumny jest inny niż varchar/nvarchar jak tekst , musimy oddać wartość kolumny jako string, a następnie przekonwertować ją jako:

update URL_TABLE
set Parameters = REPLACE ( cast(Parameters as varchar(max)), 'india', 'bharat')
where URL_ID='150721_013359670'
 3
Author: khichar.anil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-21 14:09:30

Możesz również zastąpić duży tekst szablonu wiadomości e-mail w czasie uruchamiania, Oto prosty przykład na to.

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
 0
Author: shekhar patel,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-14 06:17:04