简体   繁体   中英

Sql Query to Retrieve Path and file Name

I need to retrieve the file name with extension from a database column.

example : from c:\\abc\\bde\\hyr\\gff\\test.txt

I need to get the result like test.txt

and c:\\abc\\bde\\hyr\\gff

Please Help

Thanks

You can try this.

SELECT REVERSE(LEFT(REVERSE(columnName),CHARINDEX('\',REVERSE(columnName))-1)) AS FileName,
REVERSE(RIGHT(REVERSE(columnName),LEN(REVERSE(columnName)) - CHARINDEX('\',REVERSE(columnName)))) AS Directory

An alternative to SubString would be to use the following, using CharIndex and Reverse :

Select  Left([FilePath], Len([FilePath]) - CharIndex('\', Reverse([FilePath])))     [FolderPath],
        Right('\' + [FilePath], CharIndex('\', Reverse('\' + [FilePath])) - (1))    [FileName]

This will pull everything after the last \\ as the FileName, and everything before as the FolderPath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM