Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
String Compare in Sql Server i have two string (str1 and str2).
For Example:-

SQL
str1=mithilesh kumar
str2=mzthilesh kumar
then outPut is
error at position= 2.


str1=mithilesh kumar
str2=mzthilesh kamar
then outPut is
error at position= 2, 12,

str1=mithilesh kumar
str2=mithilesh kumar
then outPut is
error at position= 0.
Posted
Updated 16-May-13 21:23pm
v2
Comments
Sant Osha 17-May-13 3:23am    
question not clear, plz modify so that we an understand
gvprabu 17-May-13 3:24am    
U need to compare str1,str2 in sql server right. give more information abt ur problem.
Zoltán Zörgő 17-May-13 4:29am    
You want to find out at what position the strings differ? And you want the result as a string with comma separated values?

this might help you

SQL
declare @str1 varchar(10)
declare @str2 varchar(10)
set @str1='mithilesh kumar'
set @str2='mzthilesh kumar'
select case when @str1=@str2 then 'Same' else 'Different' end
 
Share this answer
 
v2
Here is a sample.

SQL
DECLARE @Str1 VARCHAR(100)
DECLARE @Str2 VARCHAR(100)
DECLARE @I INT
DECLARE @Result VARCHAR(100)

SET @Str1 = 'mithilesh kumar'
SET @Str2 = 'mzthilesh kumar'
SET @I = 1
SET @Result = 'Error At Position = '


IF PATINDEX(@Str1, @Str2) = 1
BEGIN
	-- Both strings are equal. 
	SET @Result = @Result + ' 0.'
END
ELSE
BEGIN
	-- Strings are not equal.
	WHILE @I <= LEN(@Str2)
	BEGIN

		
		IF (SUBSTRING(@Str1,@i,1) != SUBSTRING(@Str2,@i,1))
		BEGIN
			SET @Result = @Result + CAST(@I AS VARCHAR) + ', '
		END
		
		SET @I = @I + 1

	END
	--Remove the comma at the end and add a Period '.'
	SET @Result = SUBSTRING(@Result,1,LEN(@Result)-1) + '.'
END	



SELECT @Result AS [Output]


Hope this helps.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900