SQLTeam.com | Weblogs | Forums

How to get just the filename from the following string


I have the file names with the below format:


want to get just the filename.pdf part which is right after the Hyphen. upto end.in this case is pdf file.

I have over 500k filenames want to extract the filenames.

Thanks a lot for the helpful info.

declare @fn varchar(255) = '123.672-filename.pdf'
select reverse(@fn), CHARINDEX('.', reverse(@fn)), left(@fn, len(@fn) -CHARINDEX('.', reverse(@fn)))


--The " + '-' " is just in case there is no - in the original string.

SELECT RIGHT(string, CHARINDEX('-', REVERSE(string) + '-') - 1)
    SELECT '123.672-filename.pdf' AS string
) AS test_data


Thanks a lot it worked. thank you.



declare @fn varchar(255) = '123.672-filename.pdf'
select substring(@fn,charindex('-',@fn)+1,len(@fn))