Ektron Find Replace In Content Html

| | | | |

This script will search and replace specific content/phrases through the entire content table.
This script can be modified to handle different scenarios as well.

/*
search/replace script to replace characters in the content fields
of the content_text and content_teaser fields
 = *space*
€“ = —
€™ = '
€¢ = •
€¦ = :
*/
SET XACT_ABORT ON

DECLARE @otxt VARCHAR(8000)

SET @otxt = '' --string to find

DECLARE @ntxt VARCHAR(8000)

SET @ntxt = '' --replacement string

DECLARE @txtlen INT

SET @txtlen = len(@otxt)

DECLARE @ptr BINARY (16)
DECLARE @pos INT
DECLARE @id INT

BEGIN TRAN

-- begin content_html loop
WHILE (
		SELECT count(content_id)
		FROM content
		WHERE content_html LIKE '%' + @otxt + '%'
		) > 0
BEGIN
	DECLARE curs CURSOR LOCAL FAST_FORWARD
	FOR
	SELECT content_id
		,textptr(content_html)
		,PATindex('%' + @otxt + '%', content_html) - 1
	FROM content
	WHERE content_html LIKE '%' + @otxt + '%'

	OPEN curs

	FETCH NEXT
	FROM curs
	INTO @id
		,@ptr
		,@pos

	WHILE @@fetch_status = 0
	BEGIN
		PRINT 'Text found in row id=' + cast(@id AS VARCHAR) + ' at pos=' + cast(@pos AS VARCHAR) UPDATETEXT content.content_html @ptr @pos @txtlen @ntxt

		FETCH NEXT
		FROM curs
		INTO @id
			,@ptr
			,@pos
	END

	CLOSE curs

	DEALLOCATE curs
END

-- begin content_teaser loop
WHILE (
		SELECT count(content_id)
		FROM content
		WHERE content_teaser LIKE '%' + @otxt + '%'
		) > 0
BEGIN
	DECLARE curs CURSOR LOCAL FAST_FORWARD
	FOR
	SELECT content_id
		,textptr(content_teaser)
		,PATindex('%' + @otxt + '%', content_teaser) - 1
	FROM content
	WHERE content_teaser LIKE '%' + @otxt + '%'

	OPEN curs

	FETCH NEXT
	FROM curs
	INTO @id
		,@ptr
		,@pos

	WHILE @@fetch_status = 0
	BEGIN
		PRINT 'Text found in row id=' + cast(@id AS VARCHAR) + ' at pos=' + cast(@pos AS VARCHAR) UPDATETEXT content.content_teaser @ptr @pos @txtlen @ntxt

		FETCH NEXT
		FROM curs
		INTO @id
			,@ptr
			,@pos
	END

	CLOSE curs

	DEALLOCATE curs
END

COMMIT TRAN
	--rollback tran

Source: http://www.skonet.com/Articles_Archive/Helpful_Sql_Scripts_for_Ektron_CMS_400_Net.aspx

Originally Posted on October 23, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.