我需要使用 SQL Query 从完整路径解析文件名和文件路径。
例如。完整路径 - \SERVER\D$\EXPORTFILES\EXPORT001.csv
FileName Path
EXPORT001.csv \\SERVER\D$\EXPORTFILES\
我需要使用 SQL Query 从完整路径解析文件名和文件路径。
例如。完整路径 - \SERVER\D$\EXPORTFILES\EXPORT001.csv
FileName Path
EXPORT001.csv \\SERVER\D$\EXPORTFILES\
用这个 -
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]
我做了很多 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的链接
这是最简单的方法
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)
根据 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
怎么样:
reverse(LEFT(REVERSE(FileName),
Coalesce(nullif(CHARINDEX('\', REVERSE(FileName))-1, -1), len(FileName))
))
很奇怪,我知道,但这意味着我可以避免没有\
问题并且仍然内联。
使用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]
这是一个链接,有人在其中制作了与此需求相关的几个功能:
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
对于任何想要执行此操作并删除文件扩展名的人:
SELECT
LEFT(
RIGHT(<FIELD>, CHARINDEX('\', REVERSE(<FIELD>)) - 1),
LEN(RIGHT(<FIELD>, CHARINDEX('\', REVERSE(<FIELD>)) - 1)) -
CHARINDEX('.', REVERSE(<FIELD>))
)
FROM <TABLE>
请注意,这不允许在没有斜杠的情况下 - 如果是这种情况,则需要修改
select
LTRIM(
RTRIM(
REVERSE(
SUBSTRING(
REVERSE(Filename),0,CHARINDEX('\',REVERSE(Filename),0))
)))
from TblFilePath