sql server - T-SQL: Is there a way to check if the value of one variable is present in another variable -
code:
declare @a varchar(100) = ';2;9;12;13;14;16;17;21;' declare @b varchar(100) = ';2;12;13;' declare @c varchar(100) while len(@a) > 1 begin set @c = substring(@a,1,charindex(';',@a,2)) set @b += @c @c not in @b ----this statement gives problem , shows syntax error set @a = substring(@a,charindex(';',@a,2),len(@a)) select @a, @b, @c end
what i'm trying accomplish here have declared 2 variables of varchar type , assigned them value. third variable i've declared sub-string derived 1st variable. value of 3rd variable i'm trying find it's presence in 2nd variable , if it's not i'm trying concatenate 2nd one.
so first value @c is: ';2;' , should compare @b , if not present in @b should concatenate @b.
so result should in end.
@b = ';2;12;13;9;14;16;17;21;'
kindly help. thanks.
to solve problem straightforwardly, change:
set @b += @c @c not in @b
to:
set @b += case when charindex(@c, @b) > 0 '' else @c end
Comments
Post a Comment