简体   繁体   English

SQL查询以检索路径和文件名

[英]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 范例:来自c:\\abc\\bde\\hyr\\gff\\test.txt

I need to get the result like test.txt 我需要得到像test.txt这样的结果

and c:\\abc\\bde\\hyr\\gff 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 : SubString的替代方法是使用以下内容,并使用CharIndexReverse

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. 这会将最后一个\\之后的所有内容作为FileName,并将之前的所有内容作为FolderPath。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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