I am trying to figure out a way to update / replace only text between two strings.
For instance I need to be able to update the field and replace only what's in between the following script tags leaving the text before the opening script tag untouched.
I want to keep this string<script type="text/javascript" language="javascript">********</script>
I suppose REPLACE is not going to work as the text in between the script tags will vary. Is there some type of wildcard?
UPDATE Products_Joined
SET TechSpecs = REPLACE (CAST(TechSpecs AS NVARCHAR(MAX)), '<script type="text/javascript" language="javascript">********</script>', '<script type="text/javascript" language="javascript">new text</script>' )
UPDATED: with @Parkyprg answer This works but doesn't replace the closing </script>
tag.
I end up with this.
I want to keep this string new text</script>
How do we remove the closing script tag as well?
UPDATE Products_Joined
SET TechSpecs = REPLACE(CAST(TechSpecs AS NVARCHAR(MAX)),
SUBSTRING(CAST(TechSpecs AS NVARCHAR(MAX)),
CHARINDEX('<script type="text/javascript" language="javascript">',TechSpecs),
CHARINDEX('</script>',CAST(TechSpecs AS NVARCHAR(MAX))) -
CHARINDEX('<script type="text/javascript" language="javascript">',TechSpecs)
),' new text')
nvarchar(max)
in your code correlate with thesql-server-2000
tag that your question's got? SQL Server 2000 doesn't support themax
size specifier for strings. – SapwoodSELECT @@VERSION
to find out. For future questions best to say the correct version as 2000 has lots of limitations compared to 2005 and 2008 so you may well get sub optimal answers. – Heteromerousnvarchar(max)
works. If you executeDECLARE @I INT = 1
do you get an error? If not you are on 2008. 2005 would give you the errorCannot assign a default value to a local variable.
– Heteromerous