Extracting Filename from a File Path in SQL Server

Hello everyone,

In this article, I will provide information about pulling a filename from a file path in SQL Server.

In SQL Server, in some cases, we may want to extract the file name from the file path information.

You can easily do this with the help of the code below.

DECLARE @FilePath VARCHAR(200) = 'c:\temp\data\log.txt'; --You can write your file path here.
SELECT REVERSE(LEFT(REVERSE(@FilePath), CHARINDEX('\', REVERSE(@FilePath), 1) - 1)) AS [File Name];
SELECT LEFT(@FilePath, LEN(@FilePath) - CHARINDEX('\', REVERSE(@FilePath), 1) + 1) AS [File Path];

When you run the code, you will get a result like the one below.

Extracting Filename from a File Path in SQL Server

As you can see, the filename has been taken from the file path.

Good luck to everyone in business and life.

138 Views

Yavuz Selim Kart

I try to explain what I know in software and database. I am still improving myself by doing research on many programming languages. Apart from these, I am also interested in Graphic Design and Wordpress. I also have knowledge about SEO and Social media management. In short, I am a determined person who likes to work hard.

You may also like...

Don`t copy text!