如果列是Where子句中值的一部分,则匹配

时间:2012-11-03 12:20:28

标签: sql-server tsql

我有一个问题:

我正在尝试将值(url)与dbtable列UrlPart中的值进行匹配。

假设连续的一列UrlPart的值为'id = 12345'。

现在我有一个问题:

select * from products
where UrlPart [is part of] 
'http://www.domain.com/category/?id=12345&referrer=www.google.com&etcetera' 

我希望看到匹配[id = 12345 [是]的一部分'http://www.domain.com/category/?id=12345&如果是,那么它将返回产品行。

当然[是部分]不存在,但我想知道是否可以将列中的值与将为每个请求更改的url表达式相匹配。 就像一个正常的陈述,然后被尊敬。

SQL 2008 Express Edition可能会出现这种情况吗?

[UPDATE]

我测试了类似'%'+ UrlPart'%'和CharIndex,根据MS SQL的exectution计划,它们是相同的。似乎SQL在水下做同样的事情,所以两个选项都很好。

我认为CharIndex可能会更像原始(比如可能被转换)所以我现在就去那个。

谢谢大家。

3 个答案:

答案 0 :(得分:1)

select * 
from products 
where 'http://www.domain.com/category/?id=12345&referrer=www.google.com&etcetera'  
      like '%'+UrlPart+'%'

答案 1 :(得分:1)

select * 
from products
where 'http://www.domain.com/category/?id=12345&referrer=www.google.com&etcetera' like '%' + UrlPart + '%' 

答案 2 :(得分:1)

您可以使用charindex

select * from products
where charindex('http://www.domain.com...',urlPart)<>0