string 如何在sql的where子句中使用like和substring
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40588979/
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
how to use like and substring in where clause in sql
提问by sariiia
Hope one can help me and explain this query for me, why the first query return result but the second does not:
希望有人可以帮助我并为我解释这个查询,为什么第一个查询返回结果但第二个没有:
EDIT: first query:
编辑:第一个查询:
select name from Items where name like '%abc%'
second Query:
第二个查询:
select name from Items where name like substring('''%abc%''',1,10)
why the first return result but the second return nothing while
为什么第一个返回结果但第二个什么都不返回
substring('''%abc%''',1,10)='%abc%'
If there are a logic behind that, Is there another approach to do something like the second query,
如果这背后有逻辑,是否有另一种方法来做第二个查询之类的事情,
my porpuse is to transform a string like '''abc''' to 'abc' in order to use like statement,
我的 porpuse 是将像 '''abc''' 这样的字符串转换为 'abc' 以便使用 like 语句,
回答by Andy Lamb
You can concatenate strings to form your LIKE
string. To trim the first 3 and last 3 characters from a string use the SUBSTRING
and LEN
functions. The following example assumes your match string is called @input
and starts and ends with 3 quote marks that need to be removed to find a match:
您可以连接字符串以形成您的LIKE
字符串。要修剪字符串的前 3 个和后 3 个字符,请使用SUBSTRING
和LEN
函数。以下示例假设您的匹配字符串被调用@input
,并以 3 个引号开始和结束,需要删除这些引号才能找到匹配项:
select name from Items where name like '%' + SUBSTRING(@input, 3, LEN(@input) - 4) + '%'