我得到了一个很好的答案"从完整路径中解析文件名和路径"这是由@AgentSQL提供的。
答案简单而有效,但我无法理解它。代码如下:
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],
REVERSE(LEFT(REVERSE(@full_path),CHARINDEX('\', REVERSE(@full_path), 1) - 1)) [file_name]
我在单独使用时理解左,反向和charindex的功能,但如果有人能帮我理解其中的代码,我真的很感激。
答案 0 :(得分:1)
DECLARE @full_path VARCHAR(1000)
SET @full_path = '\\abc\files\india\users\dknthasdx\Dropped CUSIPs_12_10_2014.xlsx'
select REVERSE(LEFT(REVERSE(@full_path),CHARINDEX('\', REVERSE(@full_path), 1) - 1)) [FileName]
--1) REVERSE(@full_path) --Reverses the whole path : xslx.4102_01_21_sPISUC depporD\sznahtnam\sresu\aidni\selif\lns\\
--2) CHARINDEX('\', REVERSE(@full_path), 1) -- Returns 31 in this case : The index first occurence in REVERSE PATH OF /
--3) LEFT(REVERSE(@full_path),CHARINDEX('\', REVERSE(@full_path), 1) - 1) -- return the expression till first occurence of /
--4) REVERSE(LEFT(REVERSE(@full_path),CHARINDEX('\', REVERSE(@full_path), 1) - 1)) [FileName] -- reverse the String again (reversed earlier) to get the file name
答案 1 :(得分:0)
--two additional variants
DECLARE @full_path VARCHAR(1000)
SET @full_path = '\\SERVER\D$\EXPORTFILES\EXPORT001.csv'
--1 variant
SELECT REPLACE(@full_path,RIGHT(@full_path, CHARINDEX('\', REVERSE(@full_path)) - 1), '') [path] ,
RIGHT(@full_path, CHARINDEX('\', REVERSE(@full_path)) - 1) [file_name]
--2 variant
SELECT LEFT(@full_path,(LEN(@full_path) - CHARINDEX('\', REVERSE(@full_path)) + 1)) [path] ,
RIGHT(@full_path, CHARINDEX('\', REVERSE(@full_path)) - 1) [file_name]