简体   繁体   English

在SQL中将数据字符串的一部分从一列复制到另一列

[英]Copy section of data string from one column to another column in SQL

Table and (columns) in question are: 有问题的表和(列)为:

Attachment (att_name and att_path) 附件(att_name和att_path)

I make this call to Select the info I want to update: 我打电话给“选择我要更新的信息”:

select * from attachment
where att_path like '%//bahamas/attachments/images/logos/%'

I need to update the att_name column with the filename in the path above. 我需要使用上面路径中的文件名更新att_name列。 For example, if SQL finds "//bahamas/attachments/images/logos/ ABCDE.tif " I need SQL to update the att_name to replace whatever is currently in there and insert ABCDE.tif 例如,如果SQL找到“ // bahamas / attachments / images / logos / ABCDE.tif ”,我需要SQL更新att_name以替换其中的当前内容并插入ABCDE.tif

I have tried multiple different test's on just one item and I can't seem to get my SQL correct to implement a global call where this update runs on all rows where att_path like '%//bahamas/attachments/images/logos/%' 我已经对一个项目尝试了多个不同的测试,但是我似乎无法正确地执行全局调用,因此该更新将在所有att_path这样的行(例如'%// bahamas / attachments / images / logos /%')上运行的全局调用

Any advise/help is greatly appreciated. 任何建议/帮助将不胜感激。

update attachment set
       att_name = Right(att_path, CharIndex('/', Reverse(att_path)) - 1)
 where att_path like '%//bahamas/attachments/images/logos/%'

should do what you want. 应该做你想做的。 Reversing and then searching the first occurrence of / in effect searches from the end. 倒序然后搜索/从头开始实际上搜索的第一次出现。

Before running the update, however, I usually run a select to check if I hit the correct character positions when developing statements like the following, as it is easy to get off-by-one errors with string manipulations: 但是,在运行更新之前,我通常会运行一次select来检查在开发如下所示的语句时是否命中了正确的字符位置,因为使用字符串操作很容易出现一个错误

select att_path, Right(att_path, CharIndex('/', Reverse(att_path)) - 1)
  from attachment
 where att_path like '%//bahamas/attachments/images/logos/%'

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

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