By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table t (urlLocation varchar(100));
insert into t select '\path\path\files\name-1-2-B.doc';
insert into t select '\\path\files\a file name_1--.doc';
insert into t select '\path\anotherpath\file-AB.doc';
3 rows affected
with r as (
select urlLocation, Reverse(urlLocation) r
from t
),
p as (
select *,
CharIndex ('.',r)+1 s,
IsNull(NullIf(CharIndex ('-',r)-charindex ('.',r)-1,0),1) l
from r
)
select urlLocation, Reverse(Substring(r,s,l))
from p
urlLocation | (No column name) |
---|---|
\path\path\files\name-1-2-B.doc | B |
\path\files\a file name_1--.doc | - |
\path\anotherpath\file-AB.doc | AB |