Cómo reemplazar una cadena en una columna de tabla de SQL Server
Tengo una tabla (SQL Sever
) que hace referencia a rutas (UNC
o de otra manera), pero ahora la ruta va a cambiar.
En la columna path, tengo muchos registros y necesito cambiar solo una parte de la ruta, pero no toda la ruta. Y necesito cambiar la misma cadena a la nueva, en cada registro.
¿Cómo puedo hacer esto con un simple update
?
9 answers
Es así de fácil:
update my_table
set path = replace(path, 'oldstring', 'newstring')
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-12 19:47:12
UPDATE [table]
SET [column] = REPLACE([column], '/foo/', '/bar/')
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-05-02 09:47:07
Probé lo anterior, pero no dio el resultado correcto. El siguiente lo hace:
update table
set path = replace(path, 'oldstring', 'newstring') where path = 'oldstring'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
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%'
Sin la función CAST
tengo un error
Argumento el tipo de dato ntext
no es válido para el argumento 1 de la función replace
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-09 14:51:39
Puede utilizar esta consulta
update table_name set column_name = replace (column_name , 'oldstring' ,'newstring') where column_name like 'oldstring%'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-27 12:00:28
Todas las respuestas son geniales, pero solo quiero darte un buen ejemplo
select replace('this value from table', 'table', 'table but updated')
Esta instrucción SQL reemplazará la existencia de la palabra " table" (segundo parámetro) dentro de la instrucción dada (primer parámetro) con el tercer parámetro
El valor inicial es this value from table
pero después de ejecutar la función replace será this value from table but updated
Y aquí hay un ejemplo real
UPDATE publication
SET doi = replace(doi, '10.7440/perifrasis', '10.25025/perifrasis')
WHERE doi like '10.7440/perifrasis%'
Por ejemplo, si tenemos este valor
10.7440/perifrasis.2010.1.issue-1
Se convertirá en
10.25025/perifrasis.2010.1.issue-1
Espero que esto nos dé mejor visualización
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-24 16:01:20
select replace(ImagePath, '~/', '../') as NewImagePath from tblMyTable
Donde "ImagePath" es el nombre de mi columna.
"NewImagePath" es temporery nombre de la columna insted de "ImagePath"
"~/" es mi cadena actual.(antiguo string)
"../ "es mi cuerda requerida.(nueva cadena)
"tblMyTable" es mi tabla en la base de datos.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-30 14:53:48
Si el tipo de columna de destino no es varchar / nvarchar como text , necesitamos convertir el valor de la columna como cadena y luego convertirlo como:
update URL_TABLE
set Parameters = REPLACE ( cast(Parameters as varchar(max)), 'india', 'bharat')
where URL_ID='150721_013359670'
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-07-21 14:09:30
También puede reemplazar texto grande para la plantilla de correo electrónico en tiempo de ejecución, aquí hay un ejemplo simple para eso.
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-02-14 06:17:04