Copyright: copy, please indicate the source https://blog.csdn.net/weixin_39392627/article/details/86756305
env:Windows Server 2016
SQL Server 2016 SP2
Data column is stored inside the file name and path of the file name to how smoothly taken out?
1. Establish a table to store test data
command:
CREATE TABLE #DirTree
(
subdirectory NVARCHAR(200),
depth INT,
isfile BIT
)
2. Use xp_dirtree put to the test data table
command:
INSERT INTO #DirTree (subdirectory, depth, isfile)
EXECUTE master.sys.xp_dirtree ' F:\DBBackup\dbserver03\Fullbackup\',1,1
3. re-use test data to make a complete table of test data file containing the path name
command:
SELECT 'F:\DBBackup\dbserver03\Fullbackup\' + subdirectory AS FullDirectory
INTO #DirTreeFull
FROM #DirTree
4. Finally, the function CHARINDEX REVERSE and separating into a separate file name field
command:
SELECT *,
REVERSE(left(REVERSE(FullDirectory), CHARINDEX('\', REVERSE(FullDirectory)) - 1)) AS [FileName]
FROM #DirTreeFull
Item 5. The syntax below the split path
command:
SELECT *,
REVERSE(SUBSTRING(REVERSE(FullDirectory), CHARINDEX('\', REVERSE(FullDirectory)) + 1, LEN(FullDirectory))) AS [DirectoryName],
REVERSE(left(REVERSE(FullDirectory), CHARINDEX('\', REVERSE(FullDirectory)) - 1)) AS [FileName]
FROM #DirTreeFull