从完整路径解析文件名和路径

时间:2013-10-21 22:23:41

标签: sql-server tsql

我需要使用SQL Query从完整路径解析文件名和文件路径。

EG。 Fullpath - \ SERVER \ D $ \ EXPORTFILES \ EXPORT001.csv

FileName        Path
EXPORT001.csv   \\SERVER\D$\EXPORTFILES\

10 个答案:

答案 0 :(得分:51)

使用此 -

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]

答案 1 :(得分:15)

我做了很多ETL工作,我正在寻找一个我可以使用的函数,qub1n's solution非常好,除了没有反斜杠的值。这是对qub1n解决方案的一点调整,它将处理没有反斜杠的字符串:

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

样品:

    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

这是一个LINK到SqlFiddle

答案 2 :(得分:5)

根据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

测试代码:

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

答案 3 :(得分:4)

这是最简单的方法

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)

答案 4 :(得分:2)

这是一个链接,其中有人做了几个与此需求相关的功能:

  • 创建功能[dbo]。[GetFileName]
  • 创建功能[dbo]。[GetFileNameWithoutExtension]
  • 创建功能[dbo]。[GetDirectoryPath]
  • 创建功能[dbo]。[GetExtension]

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

答案 5 :(得分: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

答案 6 :(得分:1)

使用REVERSE更容易看到

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]

答案 7 :(得分:0)

select 
LTRIM(
RTRIM(
REVERSE(
SUBSTRING(
REVERSE(Filename),0,CHARINDEX('\',REVERSE(Filename),0))
)))
 from TblFilePath

答案 8 :(得分:0)

怎么样:

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

我知道这很奇怪,但这意味着我可以避免出现no \问题,并且仍然可以内联进行。

答案 9 :(得分:0)

对于想要执行此操作并修剪文件扩展名的任何人:

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

请注意,这在没有斜杠的情况下是不允许的-在这种情况下,将需要修改