Peter
Peter

Reputation: 14518

Using T-SQL to replace HTML in table not working

I'm trying to do a replace to get some unwanted content out of our Sitefinity Content blocks. To do this I've created a script to replace a term with an empty string. See for yourself:

DECLARE @SearchText nvarchar(512),
        @ReplaceText nvarchar(512)

SET @SearchText = '%<img width="19" height="16" alt="" style="margin: 0px; vertical-align: middle;border: 0px solid;" src="http://www.eua.be/uploads/RTEmagicC_d6849d8eef_44a388.gif.gif" />%';
SET @ReplaceText = '';

select * from sf_control_properties where val like @SearchText

update sf_control_properties set val = REPLACE(val, @SearchText, @ReplaceText)

It does find the content when I run the select, but the replace function doesn't seem to be doing much. I have no clue what is going on, can someone spot the error? An example database value that should be replaced is below.

<p>Europe’s universities are increasingly developing partnerships in their research and innovation missions, embracing the “Open Innovation model” of university-business collaboration and seeking to embed this is in sound project management and improved intellectual property management that reflects respective interests.</p> <p>The Responsible Partnering Initiative has been developed through close collaboration between EUA, the European Industrial Research Management Association (<a href="http://www.eirma.org/f3/cmps_index.php?page=home">EIRMA</a>), the European Association of Research and Technology Organisations (<a href="http://www.earto.org/">EARTO</a>) and the European Network of Knowledge Transfer Offices linked to Universities and Public Research Organisations (<a href="http://www.protoneurope.org/">ProTon Europe</a>).<br /> <br />
The origins of the&nbsp;<a sfref="[documents]47af5eca-84b9-6a87-9ce5-ff00009465c7" href="/Libraries/research/2004_Responsible_Partnering_-_ConferenceReportFinal.pdf?sfvrsn=0" target="_blank">Responsible Partnering Initiative</a> <img width="19" height="16" alt="" style="margin: 0px; vertical-align: middle;border: 0px solid;" src="http://www.eua.be/uploads/RTEmagicC_d6849d8eef_44a388.gif.gif" />&nbsp;date back to a major Conference which was held in 2004 which brought together the main stakeholders from universities, industry and public research organisations. As a result of the conference, a handbook based on good practices in university/industry collaborative research was published in 2005, entitled “<a sfref="[documents]4daf5eca-84b9-6a87-9ce5-ff00009465c7" href="/Libraries/research/2005_Responsible_Partnering_rp-2005-v102_2.pdf?sfvrsn=0" target="_blank">Responsible Partnering: Joining Forces in a World of Open Innovation</a>”. <img width="19" height="16" alt="" style="margin: 0px; vertical-align: middle;border: 0px solid;" src="http://www.eua.be/uploads/RTEmagicC_d6849d8eef_44a388.gif.gif" /></p> <p>A guide to better practices for collaborative research and knowledge transfer between science and industry, the Responsible Partnering Handbook was designed to help senior managers in the public and private sectors responsible for the creation, transfer and application of knowledge. It is seen as a useful step in the process of facilitating more regular interactions between European universities and industry, of building trust and establishing mutually beneficial relations, while respecting each others' core objectives.<br /> <br />
The Responsible Partnering Guidelines cover core areas that are crucial to university/industry collaboration concerning aligning interests, professional training and skills, consortia-building, intellectual property rights and patents and finally, importantly, building lasting relationships in collaborative research. The guidelines have been recognised as a pioneering European initiative in various European Commission Communications on improving knowledge transfer and the management of intellectual property rights, and have been recommended strongly for further implementation by the “<a sfref="[documents]71af5eca-84b9-6a87-9ce5-ff00009465c7" href="/Libraries/research/aho_report.pdf?sfvrsn=0" target="_blank">Aho Report on Creating an Innovative Europe</a>”. <img width="19" height="16" alt="" style="margin: 0px; vertical-align: middle;border: 0px solid;" src="http://www.eua.be/uploads/RTEmagicC_d6849d8eef_44a388.gif.gif" /></p> <p>The Responsible Partnering Guidelines is a “living document” that is revised regularly in the light of further evidence of good practices and to address new issues in university-business research collaboration. Revisions have been made through “validation workshops” organised with the support from the European Commission DG Research, to gain critical feedback on their strengths and weaknesses and potential for further development and implementation. Workshop participants were drawn from universities, industry (both large and small business enterprises), public research organisations, knowledge transfer offices and other interested parties. A Special Conference was held in Lisbon, Portugal in December 2007, and the report from the conference is available&nbsp;<a sfref="[documents]d7b05eca-84b9-6a87-9ce5-ff00009465c7" href="/Libraries/research/Responsible_Partnering_-_2007_Lisbon_Conference.pdf?sfvrsn=0" target="_blank">here</a> <img width="19" height="16" alt="" style="margin: 0px; vertical-align: middle;border: 0px solid;" src="http://www.eua.be/uploads/RTEmagicC_d6849d8eef_44a388.gif.gif" />. </p> <p>In October 2009 a fully revised&nbsp;<a sfref="[documents]b3ad5eca-84b9-6a87-9ce5-ff00009465c7" href="/Libraries/publications-homepage-list/Responsible_Partnering_Guidelines_09.pdf?sfvrsn=0">'Responsible Partnering Guidelines'</a> <img width="19" height="16" alt="" style="margin: 0px; vertical-align: middle;border: 0px solid;" src="http://www.eua.be/uploads/RTEmagicC_d6849d8eef_44a388.gif.gif" />was published which include issues such as State Aid, European Community recommendations on IPR management and the results of the EUA <a href="[Sitefinity]c2e121b1-261f-4466-b8d9-14effccd2b20">DOC-CAREERS </a>project on university-industry partnerships in doctoral research.&nbsp;The Steering Group for the Responsible Partnering Initiative contributes currently also to the <strong>EC-initiated University-Business Forum</strong> and promotes dialogue with the European Institute of Technology (EIT). </p> <p>EUA has also proposed to the European Research Area Board (ERAB) that the Responsible Partnering Guidelines should be taken forward as a best practice in the first steps to creating an “Open Innovation Charter” proposed as an ERA Milestone in the ERAB’s recommendations in its <a href="http://ec.europa.eu/research/erab/pdf/erab-first-annual-report-06102009_en.pdf">“Strategic View of the European Research Area: Preparing Europe for a New Renaissance<img width="19" height="16" alt="" style="margin: 0px; vertical-align: middle;border: 0px solid;" src="http://www.eua.be/uploads/RTEmagicC_d6849d8eef_44a388.gif.gif" />.</a></p>

Upvotes: 0

Views: 61

Answers (2)

CiucaS
CiucaS

Reputation: 2128

CREATE TABLE #Text (c1 varchar(100))

INSERT INTO #Text VALUES ('ASDF') 

INSERT INTO #Text VALUES ('BCDE')

INSERT INTO #Text VALUES ('CDE')


DECLARE @SearchText nvarchar(512),
        @ReplaceText nvarchar(512),
        @SearchReplacement nvarchar(512)

SET @SearchText = '%DF%';
SET @SearchReplacement = 'DF'
SET @ReplaceText = '';

select * from #Text where c1 like @SearchText

update #Text set c1 = REPLACE(c1, @SearchReplacement, @ReplaceText)

REPLACE can't find the text because of the '% %' you have in @SearchText. You could also use SUBSTRING to remove the '%' in @SearchText

Upvotes: 1

Peter
Peter

Reputation: 14518

I am a dumbass.

Obviously the replace statement does not understand wildcard characters like the %.

I've updated the function like so which did work.

DECLARE @SearchText nvarchar(512),
        @ReplaceText nvarchar(512)

SET @SearchText = '%<img width="19" height="16" alt="" style="margin: 0px; vertical-align: middle;border: 0px solid;" src="http://www.eua.be/uploads/RTEmagicC_d6849d8eef_44a388.gif.gif" />%';
SET @ReplaceText = '';


select * from sf_control_properties where val like @SearchText

SET @SearchText = REPLACE(@SearchText, '%', '')

SELECT 'Replacing ' + @SearchText + ' with ' + @ReplaceText

update sf_control_properties set val = REPLACE(val, @SearchText, @ReplaceText)

Upvotes: 0

Related Questions