Removing part of string in existing text in field

Dear,

I have dbo.products table where in cdescription field, I found that along with my text java script is saved which I want to remove but not the original text.

For example, in cdescription field I have text as:

"Commonly referred to as M1942 suspenders, these were issued to Paratroopers with the M1942 Jump Suit. Remember that original suspenders are short by today's standards.<input id="gwProxy" type="hidden"><!--Session data--><input onclick="jsCall();" id="jsProxy" type="hidden"><div id="refHTML"></div><input id="gwProxy" type="hidden"><!--Session data--><input onclick="jsCall();" id="jsProxy" type="hidden"><div id="refHTML"></div>"

In above I want to remove:
<input id="gwProxy" type="hidden"><!--Session data--><input onclick="jsCall();" id="jsProxy" type="hidden"><div id="refHTML"></div><input id="gwProxy" type="hidden"><!--Session data--><input onclick="jsCall();" id="jsProxy" type="hidden"><div id="refHTML"></div>

So that my final result comes;

"Commonly referred to as M1942 suspenders, these were issued to Paratroopers with the M1942 Jump Suit. Remember that original suspenders are short by today's standards."

Please advise the update sql statement to remove unwanted string without touching rest of text.

Thanks.

Nick
NickHowardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

quizwedgeCommented:
Assuming that you want to strip out anything after the first "<" character, you can do the following:

SUBSTRING(cdescription, 1, CHARINDEX('<', cdescription) - 1)

Open in new window


That will return just the first part.
0
NickHowardAuthor Commented:
Sorry, script could be anywhere either in start, middle or end of the text.

Thus requirement is to lookup the field cdescription and loop through all rows and if any of following found in the text, remove it without touching the actual text:

<!--Session data-->
<input onclick="jsCall();" id="jsProxy" type="hidden"
><div id="refHTML"></div>
<input id="gwProxy" type="hidden">

I can run update sql statement 4 times to look for each individually and remove them.

Please provide me full syntax of the update sql.

Thanks.
0
quizwedgeCommented:
If it's only those four, you'll need the replace function. It looks like

 REPLACE([column], 'oldstring', 'newstring')

Open in new window


So, in your case, it would look like

UPDATE dbo.products
SET cdescription = REPLACE(cdescription, '<!--Session data-->', '')

UPDATE dbo.products
SET cdescription = REPLACE(cdescription, '<input onclick="jsCall();" id="jsProxy" type="hidden"', '')

UPDATE dbo.products
SET cdescription = REPLACE(cdescription, '><div id="refHTML"></div>', '')

UPDATE dbo.products
SET cdescription = REPLACE(cdescription, '<input id="gwProxy" type="hidden">', '')

Open in new window


Warning, I didn't double check those statements, so I'd make a backup of your table before running them.

Also, I'm guessing that could be combined into one statement, but since this sounds like something you're going to run once, I did them as separate statements.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

NickHowardAuthor Commented:
Sorry, when I execute your proposed update sql, I get following error:

 37000(8116)[Microsoft][ODBC SQL Server Driver][SQL Server]Argument data type ntext is invalid for argument 1 of replace function.
37000(8180)[Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared. (0.00 secs)

Please advise.
0
quizwedgeCommented:
Replace doesn't work on ntext, but we can cast it to nvarchar and then cast it back. Again, I haven't tested it so backup your table, but the following should work so long as you're running SQL 2005 or later.

UPDATE dbo.products
SET cdescription = cast(REPLACE(cast(cdescription as nvarchar(max)), '<!--Session data-->', '') as ntext)

UPDATE dbo.products
SET cdescription = cast(REPLACE(cast(cdescription as nvarchar(max)), '<input onclick="jsCall();" id="jsProxy" type="hidden"', '') as ntext)

UPDATE dbo.products
SET cdescription = cast(REPLACE(cast(cdescription as nvarchar(max)), '><div id="refHTML"></div>', '') as ntext)

UPDATE dbo.products
SET cdescription = cast(REPLACE(cast(cdescription as nvarchar(max)), '<input id="gwProxy" type="hidden">', '') as ntext)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NickHowardAuthor Commented:
It worked.
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.