SQL server 2014 - using patindex

Hi,

i've the below

declare @var1 varchar(max)
declare @var2 varchar(max)
declare @var3 int

set @var1='abcdefhnjkiutyrhngjkopiuy'
set @var2='gj'

select patindex('%['+@var2+']%',@var1)
set @var3=patindex('%['+@var2+']%',@var1)

select substring(@var1,@var3,len(@var2))

i was expecting the value of 18 and 'gj' as outputs. But i'm getting 9 and 'jk'. Can someone please let me know where i could be going wrong

Remove the braces. What you are telling PADINDEX is you want either g or j.

1 Like

@djj55
Thanks for you answer. It works now.

Glad I could help.