DECLARE @x TABLE(y VARCHAR(64))
INSERT @x SELECT 'E:/My_Users//kpa1/eOReport - VSD.mrk'
UNION ALL SELECT 'E:/My_Users//krishr9/Report.mrk'
UNION ALL SELECT 'E:/My_Users//kristc/AllPAS.mrk'
UNION ALL SELECT 'E:/My_Users//kukerm/v1.mrk'
UNION ALL SELECT 'E:/My_Users//ksnedike/abcdef.mrk';
SELECT y, part = SUBSTRING(y, 2, CHARINDEX('/', y, 2)-2)
FROM (SELECT y = SUBSTRING(y, CHARINDEX('//', y) + 1, 64) FROM @x) AS z;
结果:
y part
------------------------ ----------
/kpa1/eOReport - VSD.mrk kpa1
/krishr9/Report.mrk krishr9
/kristc/AllPAS.mrk kristc
/kukerm/v1.mrk kukerm
/ksnedike/abcdef.mrk ksnedike
您还可能遇到没有尾随/
字符的情况,例如'E:/foo//bar'
- 如果是这样,那么:
SELECT y, part = SUBSTRING(y, 2, COALESCE(NULLIF(CHARINDEX('/', y, 2),0),66)-2)
FROM (SELECT y = SUBSTRING(y, CHARINDEX('//', y) + 1, 64) FROM @x) AS z;