我有旧的遗留代码,十年来一直将带有绝对路径的文件名插入数据库。正如您可能已经猜到的,路径已更改多次,现在我的列包含许多不同位置的文件名。 我愿意:
- 更新此列以仅包含文件名 + 扩展名
- 将代码更改为仅插入文件名 + 扩展名。
- 更改 getter 以获取这个新的数据库列,并期望它只是文件名 + 扩展名,并附加从属性文件检索到的完整路径
这就是 DistListName
列现在在数据库中的样子:
DistListName
D:/REPOSITORY/DistributionListFiles/DistListFile-201312241253420087_1.csv
D:\docs\upload\DocAccess\Projects\ET\Distribution\ID_LE36A12-8-17-520-edited-6rows.csv
D:\docs\upload\Temp\5thdistributionList.csv
D:\docs\upload\DocAccess\Projects\ET\Distribution\APO-FPO - 3rows.csv
D:\docs\upload\Documents and Settings\mark\Desktop\4thdistributionList.csv
D:\docs\upload\Distribution\Printed Labels\2004 July\21 jul 04.csv
D:\docs\upload\200704191301190609\TestZipCode\4thdistributionList.csv
D:\docs\upload\Documents and Settings\patel\Desktop\attachments\distributionList_25.csv
D:\docs\upload\200704230652240281\Documents and Settings\omar\Desktop\ 1-1M-34 change\ID_01T06229100000R_070305102408.csv
D:\docs\upload\200704251628340140\TestZipCode\4thdistributionList.csv
b
NULL
C:\FILES\upload\201110041235580647\fakepath\ID_2 US locations2_100311a.csv
C:\FILES\upload\201111091019410135\Documents and Settings\leslo\Desktop\DPG CSV\Good CSVs\3 Jobs - FY-12-TESTING -.csv
我有显示最终结果的 SELECT 语句,但我不确定如何将其转换为 UPDATE 语句:
select
CASE WHEN CHARINDEX('/', REPLACE(DistListName,'\','/')) > 0 THEN
RIGHT(REPLACE(DistListName,'\','/'), CHARINDEX('/', REVERSE(REPLACE(DistListName,'\','/'))) -1) ELSE DistListName END file_name
FROM myTable
输出:
DistListName
DistListFile-201312241253420087_1.csv
ID_LE36A12-8-17-520-edited-6rows.csv
5thdistributionList.csv
APO-FPO - 3rows.csv
4thdistributionList.csv
21 jul 04.csv
4thdistributionList.csv
distributionList_25.csv
ID_01T06229100000R_070305102408.csv
4thdistributionList.csv
b
NULL
ID_2 US locations2_100311a.csv
3 Jobs - FY-12-TESTING -.csv
在我的代码中,我只期望数据库列中的文件名+扩展名:
public static String getUploadPath(String dbFileName)
{
ResourceBundle bundle = ResourceBundle.getBundle("fileLocations");
return bundle.getString("DistributionListFilesPath") + dbFileName;
}
fileLocations.properties
DistributionListFilesPath=D:/projectFiles/DistributionListFiles/
有人可以帮我将这个 SELECT 语句更改为 UPDATE 语句吗?
最佳答案
你可以简化一下你的表达。
示例
Select JustName = right(DistListName,charindex('\',reverse(replace(DistListName,'/','\'))+'\')-1)
From YourTable
返回
关于sql - 通过删除绝对路径并保留文件名来更新数据库列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45885381/