SQL Sql选择包含部分字符串的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2621586/
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
Sql select rows containing part of string
提问by Maxim
I want to write a comparation procedure (t-sql) for site seo.
我想为站点 seo 编写一个比较程序 (t-sql)。
I have a table with field 'url' (nvarchar()) that contain a part of site url's. Ex: 'mysyte.com/?id=2'. Also this table for each url contains metadata, that i need to extract.
我有一个包含字段 'url' (nvarchar()) 的表,其中包含站点 url 的一部分。例如:' mysyte.com/?id=2'。每个 url 的这个表也包含我需要提取的元数据。
The main problem is that full url on site looks like 'mysyte.com/?id=2®ion=0&page=1', and i just need to ignore everething, except url in table:
主要问题是网站上的完整 url 看起来像“ mysyte.com/?id=2®ion=0&page=1”,我只需要忽略所有内容,除了表中的 url:
I mean: 'mysyte.com/?id=2' => is a part of 'mysyte.com/?id=2®ion=0&page=1'
我的意思是:“mysyte.com/?id=2”=> 是“mysyte.com/?id=2®ion=0&page=1”的一部分
采纳答案by Jonathan
SELECT *
FROM myTable
WHERE URL = LEFT('mysyte.com/?id=2®ion=0&page=1', LEN(URL))
Or use CHARINDEX http://msdn.microsoft.com/en-us/library/aa258228(v=SQL.80).aspx
或者使用CHARINDEX http://msdn.microsoft.com/en-us/library/aa258228(v=SQL.80).aspx
回答by WiseGuyEh
You can use the LIKE operator to compare the content of a T-SQL string, e.g.
您可以使用 LIKE 运算符来比较 T-SQL 字符串的内容,例如
SELECT * FROM [table] WHERE [field] LIKE '%stringtosearchfor%'.
The percent character '%' is a wild card- in this case it says return any records where [field] at leastcontains the value "stringtosearchfor".
百分比字符 '%' 是一个通配符 - 在这种情况下,它表示返回 [field]至少包含值“stringtosearchfor”的任何记录。
回答by chris
you can use CHARINDEX
in t-sql.
您可以CHARINDEX
在 t-sql 中使用。
select * from table where CHARINDEX(url, 'http://url.com/url?url...') > 0
select * from table where CHARINDEX(url, 'http://url.com/url?url...') > 0