PatIndex()匹配Select Substring

问题描述 投票:0回答:3

我试图形成正确的IfCase statement,只有当Substring函数匹配时才选择PatIndex()

以下查询部分工作。问题是PatIndex()中没有匹配它返回0然后substring函数从返回的每个其他URL返回从第3列开始的4个字符。我想跳过这些与模式不匹配的URL。我试过了

Case PatIndex('%ID=%',URL) > 1

但是我在“>”附近遇到语法错误,不知道为什么。

select distinct Url, Username, substring(Url, (PatIndex('%ID=%', Url)+3), 4) as RID 
from tblWebSiteUsage
where dateandtime > '2-16-2018 12:00:00 am'
sql-server substring
3个回答
0
投票

您只能使用WHERE子句过滤具有URLID=s:

WHERE Url LIKE '%ID=%'

0
投票

此案例语句应在从您的URL中提取ID之前检查是否存在模式匹配,如果URL中没有ID =则返回null

select 
    distinct Url, 
    Username,
    case when PatIndex('%ID=%', Url) > 0 
         then substring(Url, (PatIndex('%ID=%', Url)+3), 4) 
         else null
    end as RID 
from tblWebSiteUsage
where dateandtime > '2-16-2018 12:00:00 am'`

0
投票

您更新的查询以获取仅包含字符串'ID ='的URL。

select distinct Url, Username, substring(Url, (PatIndex('%ID=%', Url)+3), 4) as RID 
from tblWebSiteUsage
where dateandtime > '2-16-2018 12:00:00 am' and Url like '%ID=%'
© www.soinside.com 2019 - 2024. All rights reserved.