Sql Server中的字符串比较 [英] String Compare in Sql Server

查看:147
本文介绍了Sql Server中的字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sql Server中的字符串比较我有两个字符串(str1和str2)。

例如: -



String Compare in Sql Server i have two string (str1 and str2).
For Example:-

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.

推荐答案

这可能对你有所帮助



this might help you

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


这是一个示例。



Here is a sample.

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.


这篇关于Sql Server中的字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆