37

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

例如。完整路径 - \SERVER\D$\EXPORTFILES\EXPORT001.csv

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

10 回答 10

78

用这个 -

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]
于 2013-10-21T22:26:40.307 回答
21

我做了很多 ETL 工作,我一直在寻找一个我可以使用的函数,并且qub1n 的 解决方案效果很好,除了没有反斜杠的值。这是 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

这是SqlFiddle的链接

于 2015-12-08T17:31:42.553 回答
6

这是最简单的方法

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)
于 2014-04-02T10:40:50.500 回答
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
于 2015-11-29T13:03:43.703 回答
3

怎么样:

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

很奇怪,我知道,但这意味着我可以避免没有\问题并且仍然内联。

于 2019-04-05T10:56:25.900 回答
2

使用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]
于 2016-04-19T18:47:59.557 回答
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

于 2015-10-17T21:35:18.593 回答
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
于 2013-10-22T12:18:55.070 回答
0

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

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

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

于 2019-05-30T00:07:21.363 回答
0
select 
LTRIM(
RTRIM(
REVERSE(
SUBSTRING(
REVERSE(Filename),0,CHARINDEX('\',REVERSE(Filename),0))
)))
 from TblFilePath
于 2017-12-25T06:41:35.717 回答