【问题标题】:UPDATE db columns by removing absolute path and leaving file name in place通过删除绝对路径并保留文件名来更新 db 列
【发布时间】:2017-08-25 16:02:45
【问题描述】:

我有十年来一直在将具有绝对路径的文件名插入数据库的旧代码。正如您可能已经猜到的那样,路径已经更改了很多次,现在我的列中包含许多不同位置的文件名。 我想:

  1. 更新此列以仅包含文件名 + 扩展名
  2. 从那时起将代码更改为仅插入文件名 + 扩展名。
  3. 更改 getter 以获取这个新的 db 列,并期望它只是文件名 + 扩展名,并附加从属性文件中检索到的完整路径

这就是DistListName 列现在在 db 中的样子:

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

在我的代码中,我只期望 db 列中的文件名 + 扩展名:

public static String getUploadPath(String dbFileName)
{
    ResourceBundle bundle = ResourceBundle.getBundle("fileLocations");
    return bundle.getString("DistributionListFilesPath") + dbFileName;
}

fileLocations.properties
    DistributionListFilesPath=D:/projectFiles/DistributionListFiles/

谁能帮我把这个 SELECT 语句改成 UPDATE 语句?

【问题讨论】:

    标签: sql sql-server tsql sql-update


    【解决方案1】:

    你可以简化一下你的表达方式。

    示例

    Select JustName = right(DistListName,charindex('\',reverse(replace(DistListName,'/','\'))+'\')-1)
     From  YourTable
    

    退货

    【讨论】:

    • 要进行更新,只需将其更改为 UPDATE YourTable SET DistListName = right(DistListName,charindex('\',reverse(replace(DistListName,'/','\'))+'\')-1)你打败了我@john-cappelletti
    • @LaughingVergil 你是对的。我倾向于不提供实际更新,因为我宁愿 OP 在销毁原始数据之前验证结果。
    猜你喜欢
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2021-12-11
    • 2012-08-07
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多