I don't think that when comparing procedure text you can do it without getting rid of the spaces or you'd probably get a lot of false posititves. Here's one , I bet there might be a clever way to do it (maybe even using bitwise operation on the ctext column) however this is just your plain old brute force effort... CREATE FUNCTION dbo.Compare(@a nvarchar(4000),@b nvarchar(4000))RETURNS @diff TABLE(difference_near nvarchar(50))ASBEGINdeclare @trima nvarchar(4000)declare @trimb nvarchar(4000)declare @origlena intdeclare @origlenb intdeclare @lena intdeclare @lenb intdeclare @chara nchar(1)declare @charb nchar(1)declare @curpos intdeclare @minlen intdeclare @factora numeric(9,5)declare @factorb numeric(9,5)declare @pretexta intdeclare @pretextb intdeclare @postexta intdeclare @postextb intdeclare @resultsa nvarchar(50)declare @resultsb nvarchar(50)select @origlena = LEN(@a), @origlenb = LEN(@b)select @trima = REPLACE(REPLACE(REPLACE(@a,' ',''),CHAR(10),''),CHAR(13),''), @trimb = REPLACE(REPLACE(REPLACE(@b,' ',''),CHAR(10),''),CHAR(13),'')select @lena = LEN(@trima), @lenb = LEN(@trimb)If @trima<>@trimbbeginselect @minlen = case when @lena<=@lenb then @lena else @lenb endset @curpos = 1 WHILE @curpos <= @minlen BEGIN SELECT @chara=SUBSTRING(@trima, @curpos, 1),@charb=SUBSTRING(@trimb, @curpos, 1) IF (UPPER(@chara)<>UPPER(@charb)) BEGIN /* This factoring bit is because we have stripped spaces and CR LF and if we return substring based on @curpos we might well miss the difference because of the missing spaces so we add a few chars based on the difference between the trimmed length of the string and the original length we saved at the start - its very rough but seems to work in most situations */ set @factora = cast((@origlena/@lena) as numeric(9,5)) set @pretexta = CASE when @curpos<20 then 1 else CEILING(@curpos*@factora) end set @factorb = cast((@origlenb/@lenb) as numeric(9,5)) set @pretextb = CASE when @curpos<20 then 1 else CEILING(@curpos*@factorb) end IF @lena<50 set @postexta = @lena ELSE set @postexta = 50 IF @lenb<50 set @postextb = @lenb ELSE set @postextb = 50 SELECT @resultsa = '....' + SUBSTRING(@a,@pretexta,@postexta) + '....', @resultsb = '....' + SUBSTRING(@b,@pretextb,@postextb) + '....' BREAK END ELSE SET @curpos = @curpos + 1 CONTINUE ENDENDINSERT @diff SELECT @resultsa UNION SELECT @resultsbRETURNEND
For your example this gives these results select * from dbo.Compare(@a,@b)
difference_near -------------------------------------------------- ....er has any certainty where he is going. Practi....er has any certainty where she is going. Pract
I haven't tested it much so feel free to rip it to shreds
HTHJasper SmithEdited by - jasper_smith on 06/28/2002 20:21:29