T-SQL: Get Filename from Filepath
Many times applications store operating system paths to files in the database. And sometimes need to get only Filename portion out of Filepath column in MSSQL. How to do that ?
Disclaimer: I know - it's much easier to get the filename using C#, however many times we need to get the list of filenames filtered/grouped by some criteria.
Since there is no split function analog in T-SQL - I will use SUBSTRING, REVERSE and CHARINDEX to manipulate strings:
SELECT REVERSE(SUBSTRING(REVERSE(@fpath), 0, CHARINDEX('\', REVERSE(@fpath), 1)))
Enjoy :)
Friday, June 27, 2008 3:13 AM