SQLTeam.com | Weblogs | Forums

SQL server 2014 - using patindex

tsql
sql2014

#1

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


#2

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


#3

@djj55
Thanks for you answer. It works now.


#4

Glad I could help.