2013-10-21 168 views
20

我需要使用SQL查詢從完整路徑解析文件名和文件路徑。從完整路徑解析文件名和路徑

例如, FULLPATH - \ SERVER \ d $ \ EXPORTFILES \ EXPORT001.csv

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

回答

37

使用此 -

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] 
+0

感謝您提供快速回復。有效! – Don

+18

爲什麼不只是:RIGHT(YOUR_PATH,CHARINDEX('\',REVERSE(YOUR_PATH))-1) –

+0

我已經測試過它,它似乎足夠。唯一的缺點是,這隻適用於反斜槓(如果可用的話在.NET中爲Path.GetFileName(path))。 – qub1n

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 
3

這是最簡單的方法

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

回答斯特凡·斯泰格爾基於評論:

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 
10

我做了很多ETL工作,我一直在尋找,我可以用和qub1n'ssolution作品除了值非常好沒有反斜槓的功能。這裏是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

這就是解決方案! ;) – SQueek

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] 
0
select 
LTRIM(
RTRIM(
REVERSE(
SUBSTRING(
REVERSE(Filename),0,CHARINDEX('\',REVERSE(Filename),0)) 
))) 
from TblFilePath