如何使用喜欢的名字搜索任何地方 [英] how to search anywhere in name using like

查看:79
本文介绍了如何使用喜欢的名字搜索任何地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询



......

来自[PaymentTracker1]

WHERE [名称] LIKE'%'+ @ SearchTerm +'%'或@SearchTerm =''

SELECT @RecordCount = COUNT(*)

FROM #Results ... 。





它只搜索姓名的开头,





如何搜索名称中的任何地方

解决方案

这样可以搜索名称中的任何位置:'%X%'将查找带有'的所有行' X'在价值的任何地方。

我只用我的一张桌子进行了双重检查:

  DECLARE   @ SearchTerm   NVARCHAR (MAX)
SET @ SearchTerm = ' X'
SELECT * FROM MyTable
WHERE [Name] LIKE ' %' + @ SearchTerm + ' %' @ SearchTerm = ' '


如果我是你,我会查看你的其余查询。


i have following query

......
FROM [PaymentTracker1]
WHERE [Name] LIKE '%' + @SearchTerm + '%' OR @SearchTerm = ''
SELECT @RecordCount = COUNT(*)
FROM #Results....


it is searching only starting of name ,


how to search anywhere in name

解决方案

That does search anywhere in the name: '%X%' will find all rows with an 'X' anywhere in the value.
I just double checked with one of my tables:

DECLARE @SearchTerm NVARCHAR(MAX)
SET @SearchTerm = 'X'
SELECT * FROM MyTable
WHERE [Name] LIKE '%' + @SearchTerm + '%' OR @SearchTerm = ''

And it retruned exactly the results I expected.

I'd look at the rest of your query, if I was you.


这篇关于如何使用喜欢的名字搜索任何地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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