How to Extract Filename from Path using SQL Functions
In this sql tutorial, t-sql developers can find sql codes for extracting file name from fullpath of a file.
No matter whether the file path is stored in a sql table column or full filepath is supplied as a sql variable or sql string parameter, sql developers can use either sql functions to parse file name or use sql string functions in order to extract file name.
Here is sample t-sql code that sql developers can use to extract filename from path given as sql string variable or extract filename from path stored in sql table column.
In this SQL SELECT statement, sql string functions like CHARINDEX, SUBSTRING and REVERSE functions are used.
Let's create a sample sql database table and insert sample records for the sql sample to extract file name from file path.
Here is the sample sql codes which works on the above sql sample data and parse filename from full filepath.
And the output of the above sql script which sql parse file path string value for file name is as follows.
Here is user function source code of sample sql functions to extract file name from path.
Here is a sample sql function call which parse file name from a given file path and returns the file name to the sql user.
And the above sample function call code returns the "SQLBackup.mdf" as the output for the SELECT statement.