简体   繁体   English

如何替换SQL Server表列中的字符串

[英]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. 我有一个表( SQL Sever )引用路径( UNC或其他),但现在路径将会改变。

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 ? 如何通过简单的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 没有CAST功能,我收到了一个错误

Argument data type ntext is invalid for argument 1 of replace function. 参数数据类型ntextreplace函数的参数1无效。

您可以使用此查询

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 此SQL语句将使用第三个参数替换给定语句(第一个参数)中“table”(第二个参数)一词的存在

the initial value is this value from table but after executing replace function it will be this value from table but updated 初始值是this value from table但是在执行replace函数之后,它将是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. 其中“ImagePath”是我的列名。
"NewImagePath" is temporery column Name insted of "ImagePath" “NewImagePath”是临时列名“ImagePath”的名称
"~/" is my current string.(old string) “〜/”是我当前的字符串。(旧字符串)
"../" is my requried string.(new string) “../”是我需要的字符串。(新字符串)
"tblMyTable" is my table in database. “tblMyTable”是我在数据库中的表。

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: 如果目标列类型不是varchar / nvarchar之类的文本 ,我们需要将列值转换为字符串,然后将其转换为:

update URL_TABLE
set Parameters = REPLACE ( cast(Parameters as varchar(max)), 'india', 'bharat')
where URL_ID='150721_013359670'

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. 这里column_name指的是您要更改的列。

Hope it will work. 希望它会奏效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM