36

I need to parse file name and file path from full path using SQL Query.

Eg. Fullpath - \SERVER\D$\EXPORTFILES\EXPORT001.csv

FileName        Path
EXPORT001.csv   \\SERVER\D$\EXPORTFILES\
Don
  • 373
  • 1
  • 3
  • 5

10 Answers10

74

Use this -

DECLARE @full_path VARCHAR(1000)
SET @full_path = '\\SERVER\D$\EXPORTFILES\EXPORT001.csv'

SELECT LEFT(@full_path,LEN(@full_path) - charindex('\',reverse(@full_path),1) + 1) [path], 
       RIGHT(@full_path, CHARINDEX('\', REVERSE(@full_path)) -1)  [file_name]
AgentSQL
  • 2,480
  • 15
  • 21
21

I do a lot of ETL work and I was looking for a function that I could use and qub1n's solution works very good except for values without a back slash. Here is a little tweak of qub1n's solution that will handle strings without back slashes:

Create FUNCTION fnGetFileName
(
    @fullpath nvarchar(260)
) 
RETURNS nvarchar(260)
AS
BEGIN
    IF(CHARINDEX('\', @fullpath) > 0)
       SELECT @fullpath = RIGHT(@fullpath, CHARINDEX('\', REVERSE(@fullpath)) -1)
       RETURN @fullpath
END

Samples:

    SELECT [dbo].[fnGetFileName]('C:\Test\New Text Document.txt') --> New Text Document.txt
    SELECT [dbo].[fnGetFileName]('C:\Test\Text Docs\New Text Document.txt') --> New Text Document.txt
    SELECT [dbo].[fnGetFileName]('New Text Document.txt') --> New Text Document.txt
    SELECT [dbo].[fnGetFileName]('\SERVER\D$\EXPORTFILES\EXPORT001.csv') --> EXPORT001.csv

Here is a LINK to SqlFiddle

Community
  • 1
  • 1
Mark Kram
  • 5,368
  • 7
  • 49
  • 68
5

Here is the simplest way

DECLARE @full_path VARCHAR(1000)
SET @full_path = '\\SERVER\D$\EXPORTFILES\EXPORT001.csv'
SELECT  LEFT(@full_path, LEN(@full_path) - CHARINDEX('\', REVERSE(@full_path)) - 1),
        RIGHT(@full_path, CHARINDEX('\', REVERSE(@full_path)) - 1)
sms247
  • 4,081
  • 4
  • 29
  • 44
5

Answer based on comment by Stefan Steiger:

Create FUNCTION GetFileName
(
 @fullpath nvarchar(260)
) 
RETURNS nvarchar(260)
AS
BEGIN
DECLARE @charIndexResult int
SET @charIndexResult = CHARINDEX('\', REVERSE(@fullpath))

IF @charIndexResult = 0
    RETURN NULL 

RETURN RIGHT(@fullpath, @charIndexResult -1)
END
GO

Test code:

DECLARE @fn nvarchar(260)

EXEC @fn = dbo.GetFileName 'AppData\goto\image.jpg'
PRINT @fn -- prints image.jpg

EXEC @fn = dbo.GetFileName 'c:\AppData\goto\image.jpg'
PRINT @fn -- prints image.jpg

EXEC @fn = dbo.GetFileName 'image.jpg'
PRINT @fn -- prints NULL
Tomas Kubes
  • 20,134
  • 14
  • 92
  • 132
2

Here's a link where someone made several functions related to this need:

  • CREATE FUNCTION [dbo].[GetFileName]
  • CREATE FUNCTION [dbo].[GetFileNameWithoutExtension]
  • CREATE FUNCTION [dbo].[GetDirectoryPath]
  • CREATE FUNCTION [dbo].[GetExtension]

http://www.codeproject.com/Tips/866934/Extracting-the-filename-from-a-full-path-in-SQL-Se?msg=5145303#xx5145303xx

MattSlay
  • 6,877
  • 4
  • 39
  • 52
2

Using REVERSE is easier to see

DECLARE @full_path VARCHAR(1000)
SET @full_path = '\\SERVER\D$\EXPORTFILES\EXPORT001.csv'

select REVERSE(LEFT(REVERSE(@full_path),CHARINDEX( '\',REVERSE(@full_path))-1)) as [FileName],
       replace(@full_path, REVERSE(LEFT(REVERSE(@full_path),CHARINDEX( '\',REVERSE(@full_path))-1)),'') as [FilePath]
Aleksej
  • 21,858
  • 5
  • 28
  • 36
2

How about:

reverse(LEFT(REVERSE(FileName), Coalesce(nullif(CHARINDEX('\', REVERSE(FileName))-1, -1), len(FileName)) ))

Weird, I know, but it means I can avoid the no \ issue and still do it inline.

Reema Parakh
  • 1,147
  • 1
  • 15
  • 39
LJ Hogan
  • 21
  • 1
1
Declare @filepath Nvarchar(1000)
Set @filepath = 'D:\ABCD\HIJK\MYFILE.TXT'

    --Using Left and Right
    Select LEFT(@filepath,LEN(@filePath)-CHARINDEX('\',REVERSE(@filepath))+1) Path,
        RIGHT(@filepath,CHARINDEX('\',REVERSE(@filepath))-1) FileName

    -- Using Substring      
    Select SUBSTRING(@filepath,1,LEN(@filepath)-CHARINDEX('\',REVERSE(@filepath))+1) Path,
        REVERSE(SUBSTRING(REVERSE(@filepath),1,CHARINDEX('\',REVERSE(@filepath))-1)) FileName
Mark Kram
  • 5,368
  • 7
  • 49
  • 68
Ali
  • 11
  • 1
0
select 
LTRIM(
RTRIM(
REVERSE(
SUBSTRING(
REVERSE(Filename),0,CHARINDEX('\',REVERSE(Filename),0))
)))
 from TblFilePath
0

For anyone that wants to perform this operation and also trim off the file extension:

SELECT
    LEFT(
        RIGHT(<FIELD>, CHARINDEX('\', REVERSE(<FIELD>)) - 1),
        LEN(RIGHT(<FIELD>, CHARINDEX('\', REVERSE(<FIELD>)) - 1)) -
          CHARINDEX('.', REVERSE(<FIELD>))
    )
FROM <TABLE>

Note that this doesn't allow for when there are no slashes - it will need modification if this is the case

Cameron Forward
  • 573
  • 5
  • 12