Using numbers table you can accomplish this,all you have to do is create numbers table..
declare @string varchar(max)
set @string='_'+'XYZ_3244H_V65_C005_Z1234567455_P_F_20160127_0137.txt'+'_'
select
substring (@string,n+1,charindex('_',@string,n+1)-n-1)
from numbers
where
substring(@string,n,1)='_'
and n<len(@string)
you also can use rownumber to select values in any place like below..
select
substring (@string,n+1,charindex('_',@string,n+1)-n-1),row_number() over (order by charindex('_',@string,n+1)) as rn
from numbers
where
substring(@string,n,1)='_'
and n<len(@string)
Then use a cte to get a number at any place like
;with cte
as
(
select
substring (@string,n+1,charindex('_',@string,n+1)-n-1) as splitstrig,row_number() over (order by charindex('_',@string,n+1)) as rn
from numbers
where
substring(@string,n,1)='_'
and n<len(@string)
)
select * from cte where rn=5