Q:

Get substring value after last occurrence of "_" in sql database

I want to get value after last occurrence of "_" so I am using below approach and it is giving me "BSARST.G041720151200" which is correct.
Is their any better way of doing this or below approach is decent enough?
declare @var varchar(max)='\\IPC-NAS03.ads.aexp.com\US022868_E3\BatchFilings\BatchArchive\FIU2FINCEN_BSARST.G041720151200'
select right(@var,charindex('_',reverse(@var))-1)

A:

I would only add a "fail-safe".  Notice the +'_'  this way it will not throw an error if no _ is found
Example
select right(@var,charindex('_',reverse(@var)+'_')-1)

Or NULL the string if not found via nullif()
select right(@var,nullif(charindex('_',reverse(@var)),0)-1)

