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;