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.