Вы можете использовать рекурсивный CTE для разделения значения строки иерархии.
declare @T table
(
ID int identity,
emp_name varchar(25),
[path] varchar(150)
)
insert into @T values
('Albert', 'Albert'),
('John', 'Albert/John'),
('Chuck', 'Albert/Chuck'),
('Tom', 'Albert/John/Tom'),
('Frank', 'Frank')
declare @EmpName varchar(25) = 'Tom'
;with cte(Sort, P1, P2, [path]) as
(
select 1,
1,
charindex('/', [path]+'/', 1),
[path]
from @T
where emp_name = @EmpName
union all
select Sort+1,
P2+1,
charindex('/', [path]+'/', C.P2+1),
[path]
from cte as C
where charindex('/', [path]+'/', C.P2+1) > 0
)
select substring([path], P1, P2-P1)
from cte
order by Sort
Результат:
(No column name)
Albert
John
Tom
Проверьте запрос здесь: http://data.stackexchange.com/stackoverflow/q/101383/
Еще одну вещь, которую вы можете попробовать
select T2.emp_name
from @T as T1
inner join @T as T2
on '/'+T1.[path]+'/' like '%/'+T2.emp_name+'/%' and
T2.emp_name <> @EmpName
where T1.emp_name = @EmpName
http://data.stackexchange.com/stackoverflow/q/101518/get-hierarchy-with-join-using-like