Это можно сделать с помощью комбинации reverse
, substring
и charindex
в сочетании с cross apply
. Найдите первое, второе (и до n-го вхождения) вхождение символа, используя серию cross apply
s, а затем извлеките необходимую подстроку.
select
reverse(substring(reverse(str), p1.pos+1, p2.pos-p1.pos-1)) as between_first_sec_from_last
,reverse(substring(reverse(str), 1, p2.pos- 1)) as upto_second_from_last
from (select 'A fox Jumped/ in to the water/ and fought with crocodile/ and then went back/ to jungle and slept' as str) t
cross apply (select (charindex('/', reverse(str)))) as p1(pos)
cross apply (select (charindex('/', reverse(str), p1.pos+1))) as p2(pos)