查找具有特定字符串位置的字符匹配的计算机

时间:2016-05-19 20:45:23

标签: sql-server string sql-server-2012 match

我正在处理一个查询,我只想返回第7个字符所在的计算机名称" K"

select distinct s.Name0 as 'Computer Name', 
s.User_Name0 as 'Last Logon user', 
os.Caption0 as 'Operating System',
u.Full_User_Name0 as 'Full User Name',
s.AD_Site_Name0 as 'Site'


from v_R_System s
full outer join v_R_User u on u.User_Name0 like s.User_Name0
full outer join v_GS_OPERATING_SYSTEM os on os.ResourceID = s.ResourceID
where os.Caption0 like '%Enterprise%'
and os.Caption0 not like '%server%'

我试图返回所有运行企业的工作站,这些工作站有一个" K"作为第7个字符,除了我的另外两个陈述。

1 个答案:

答案 0 :(得分:2)

完整的答案取决于您的SQL方言,但您可能希望在where子句中使用类似T-SQL的CHARINDEX函数:

where os.Caption0 like '%Enterprise%'
and os.Caption0 not like '%server%'
AND CHARINDEX('K', s.Name0) = 7

This is a great article that details your other string-manipulation options in T-SQL,其中一些适用于其他方言。