
/****** Object:  StoredProcedure [vefn_RemoveExtraText] ******/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[vefn_RemoveExtraText]') AND OBJECTPROPERTY(id,N'IsScalarFunction') = 1)
    DROP FUNCTION [vefn_RemoveExtraText];
GO
	
/*
select text, .dbo.vefn_RemoveExtraText(text,2,0,0) StrippedText from Contents where ContentID=373
select text, .dbo.vefn_RemoveExtraText(text,1,0,0) StrippedText from Contents where ContentID=373
select text, .dbo.vefn_RemoveExtraText(text,0,0,0) StrippedText ,.dbo.vefn_FirstLink(text,0), PATINDEX('%[' + nchar(9516) + nchar(9574) + char(21) + ']%' , text)  from Contents where ContentID=373
select .dbo.vefn_RemoveExtraText('\b Bold\b0',0,0,0)
select .dbo.vefn_RemoveExtraText('\b Bold\b0',0,1,0)
select .dbo.vefn_RemoveExtraText('A\u1?B\u12?C\u123?D\u1234?E',0,0,0)
select .dbo.vefn_RemoveExtraText('A\u1?B\u12?C\u123?D\u1234?E',0,0,1)
*/


CREATE FUNCTION [dbo].[vefn_RemoveExtraText](@text nvarchar(MAX),@includeLink int, @includeRtfFormatting int , @includeSpecialCharacters int)
RETURNS varchar(MAX)
AS
BEGIN
DECLARE @index int
DECLARE @index2 int
SET @index = .dbo.vefn_FirstLink(@text,@includeLink)
while (@index != 0)
  BEGIN
		SET @index2 = CHARINDEX('\v0' , @text)
		SET @text = substring(@text,1,@index-1) + substring(@text,@index2+3,len(@text))
		SET @index = .dbo.vefn_FirstLink(@text,@includeLink)
 END
if(@includeRtfFormatting=0)
	-- Remove Rtf Formatting
	BEGIN
			SET @text = Replace(@text, '\b0 ', '');
			SET @text = Replace(@text, '\b ', '');
			SET @text = Replace(@text, '\ul0 ', '');
			SET @text = Replace(@text, '\ul ', '');
			SET @text = Replace(@text, '\i0 ', '');
			SET @text = Replace(@text, '\i ', '');
			SET @text = Replace(@text, '\super ', '');
			SET @text = Replace(@text, '\sub ', '');
			SET @text = Replace(@text, '\nosupersub ', '');
			SET @text = Replace(@text, '\b0', '');
			SET @text = Replace(@text, '\b', '');
			SET @text = Replace(@text, '\ul0', '');
			SET @text = Replace(@text, '\ul', '');
			SET @text = Replace(@text, '\i0', '');
			SET @text = Replace(@text, '\i', '');
			SET @text = Replace(@text, '\super', '');
			SET @text = Replace(@text, '\sub', '');
			SET @text = Replace(@text, '\nosupersub', '');
	END
if(@includeSpecialCharacters=0)
  -- Remove Special Characters
  BEGIN
		SET @index = PATINDEX('%\u[0-9]?%',@text)
		while(@index != 0)
			BEGIN
				SET @text = substring(@text,1,@index-1) + substring(@text,@index+4,len(@text))
				SET @index = PATINDEX('%\u[0-9]?%',@text)
			END
		SET @index = PATINDEX('%\u[0-9][0-9]?%',@text)
		while(@index != 0)
			BEGIN
				SET @text = substring(@text,1,@index-1) + substring(@text,@index+5,len(@text))
				SET @index = PATINDEX('%\u[0-9][0-9]?%',@text)
			END
		SET @index = PATINDEX('%\u[0-9][0-9][0-9]?%',@text)
		while(@index != 0)
			BEGIN
				SET @text = substring(@text,1,@index-1) + substring(@text,@index+6,len(@text))
				SET @index = PATINDEX('%\u[0-9][0-9][0-9]?%',@text)
			END
		SET @index = PATINDEX('%\u[0-9][0-9][0-9][0-9]?%',@text)
		while(@index != 0)
			BEGIN
				SET @text = substring(@text,1,@index-1) + substring(@text,@index+7,len(@text))
				SET @index = PATINDEX('%\u[0-9][0-9][0-9][0-9]?%',@text)
			END
		SET @index = PATINDEX('%\''[0-9A-Fa-f][0-9A-Fa-f]%',@text)
		while(@index != 0)
			BEGIN
				SET @text = substring(@text,1,@index-1) + substring(@text,@index+4,len(@text))
				SET @index = PATINDEX('%\''[0-9A-Fa-f][0-9A-Fa-f]%',@text)
			END

  END
RETURN @text
END
GO
-- Display the status of Proc creation
IF (@@Error = 0) PRINT 'ScalarFunction Creation: vefn_RemoveExtraText Succeeded'
ELSE PRINT 'ScalarFunction Creation: vefn_RemoveExtraText Error on Creation'
GO
