在 sql server 中使用 LIKE 通配符进行查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7804352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Queries using LIKE wildcards in sql server
提问by Ali_dotNet
I want to perform a small SQL server search in my ASP.NET web project, my database is not big so I think it's better not to use full-text-search I want to perform a simple search like this:
我想在我的 ASP.NET web 项目中执行一个小的 SQL 服务器搜索,我的数据库不大所以我认为最好不要使用全文搜索我想执行一个这样的简单搜索:
select * from mytable where columnA LIKE '%something%'
I can use = in the following way:
我可以通过以下方式使用 =:
select * from mytable where columnA='"+myVariable+"'
but how can I use a variable instead of %something%
in LIKE phrase?
is it correct? LIKE '"+%myVariable%+"'
???
但是如何使用变量而不是%something%
LIKE 短语?这是正确的吗?LIKE '"+%myVariable%+"'
???
I use VS2010,C#
我使用 VS2010,C#
thanks
谢谢
回答by Aziz Shaikh
Use:
用:
where columnA LIKE '%' + myVariable + '%'
回答by sll
WHERE
columnName LIKE '%' + myVarCharVariable +'%'
回答by Naveed
Try this query:
试试这个查询:
select * from tablename where colname like '%' + @varname + '%'
Hope it helps.
希望能帮助到你。
回答by Purplegoldfish
I just tried this and found you can do as below:
我刚试过这个,发现你可以做如下:
SELECT * FROM whatever WHERE column LIKE '%'+@var+'%'
回答by YetiSized
DECLARE @myVariable varchar(MAX)
SET @myVariable = 'WhatYouAreLookingFor'
SELECT * FROM mytable
WHERE columnA LIKE '%' + @myVariable + '%'
回答by Chad Portman
In case someone else stumbles into this post like I did. On SSMS 2012 with a SQL 2012 Server back end I was able to use code as follows without issues.
以防其他人像我一样偶然发现这篇文章。在带有 SQL 2012 Server 后端的 SSMS 2012 上,我可以毫无问题地使用如下代码。
Declare @MyVariable
Set @MyVariable = '%DesiredString%'
Select *
From Table_A
Where Field_A like @MyVariable
Then each time you want to change the Desired String just change it at the Set statement.
然后每次您想更改所需字符串时,只需在 Set 语句中更改它。
I know this post was made prior to 2012 that is why I am mentioning it in case someone with a newer setup looks up this post.
我知道这篇文章是在 2012 年之前发布的,这就是为什么我要提到它,以防有更新设置的人查找这篇文章。
回答by Pieter
Well you could do something like:
那么你可以做这样的事情:
var query = "SELECT * FROM MyTable WHERE columnA LIKE '%" + myVariable + "%'";