declare @Category table (CategoryName varchar(50))
insert into @Category (CategoryName) values('phone blue')
insert into @Category (CategoryName) values('phone pink')
insert into @Category (CategoryName) values('phone big')
insert into @Category (CategoryName) values('phone big bang')
insert into @Category (CategoryName) values('bad phone blue')
insert into @Category (CategoryName) values('bad phone pink')
insert into @Category (CategoryName) values('bad phone but good')
insert into @Category (CategoryName) values('phone')
insert into @Category (CategoryName) values('silly phone')
insert into @Category (CategoryName) values('lost phone ')
insert into @Category (CategoryName) values('white phone user')
insert into @Category (CategoryName) values('some device')
insert into @Category (CategoryName) values('phoner homer')
insert into @Category (CategoryName) values('space phone ')
insert into @Category (CategoryName) values('jam phone hex doc')
declare @Formword varchar(50) = 'phone';
select CategoryName
--, charindex(@FormWord,Categoryname)
--, charindex(@FormWord,Categoryname) + len(@FormWord)-1
--, charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord))
--, ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord)))))
--, charindex(' ',ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord))))))
--, left(ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord))))),charindex(' ',ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord)))))))
, case when charindex(' ',ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord)))))) = 0
then @Formword + ' ' + ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord)))))
else
@Formword + ' ' + left(ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord))))),charindex(' ',ltrim(right(categoryname,(len(categoryname)-charindex(' ',CategoryName,charindex(@FormWord,Categoryname) + len(@FormWord)))))))
end [next word]
from @Category
where 1 = 1
and CategoryName like '%' + @Formword + '%'
and len(CategoryName) > len(@Formword) + 1
and charindex(@FormWord,Categoryname) + len(@FormWord) < len(Categoryname)
這對於關係數據庫來說確實不是一個合適的問題。 –
看起來像'我的手機%'類'我的例子 – jean