SQLTeam.com | Weblogs | Forums

How to get just the filename from the following string


#1

I have the file names with the below format:

123.672-filename.pdf

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.


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

#3

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

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

#4

Thanks a lot it worked. thank you.


#5

or

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