我试图形成正确的If
或Case 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'
您只能使用WHERE子句过滤具有URL
的ID=
s:
WHERE Url LIKE '%ID=%'
此案例语句应在从您的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'`
您更新的查询以获取仅包含字符串'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=%'