简体   繁体   English

使用值从另一列中删除字符串的一部分

[英]Removing part of a String using value from another column

I have a field that contains file paths to attachments, contained within the filename is the attachments "AttachmentID" which gets auto appended, but in some cases this ID is duplicated which is causing problems when my front-end tries to find the attachment. 我有一个包含附件文件路径的字段,文件名中包含附件“ AttachmentID”,该附件会自动附加,但是在某些情况下,此ID是重复的,这在我的前端尝试查找附件时会引起问题。 I want to remove the duplicate ID. 我要删除重复的ID。

I'm thinking the best way to do this is using REPLACE but I don't know how I can tell SQL find the AttachmentID within the Path 我在想最好的方法是使用REPLACE,但是我不知道如何告诉SQL在Path中找到AttachmentID

Here's what I've written to find the records: 这是我为查找记录而写的内容:

SELECT Path
FROM [Attachments].[dbo].[Attachments]
WHERE [Path] LIKE CONCAT ('%','-',[AttachmentID],'-','%')

Ie \\\\SERVERNAME\\X\\FILEPATH\\ATTACHMENT\\01928-01928-Filename.JPG \\\\SERVERNAME\\X\\FILEPATH\\ATTACHMENT\\01928-01928-Filename.JPG

I want it to read: \\\\SERVERNAME\\X\\FILEPATH\\ATTACHMENT\\01928-Filename.JPG 我希望它显示为: \\\\SERVERNAME\\X\\FILEPATH\\ATTACHMENT\\01928-Filename.JPG

That number I'm removing is also stored independently in another column called AttachmentID. 我要删除的数字也独立存储在另一个列为AttachmentID的列中。

I think I may have answered my own question, sorry! 我想我可能已经回答了我自己的问题,对不起!

UPDATE [Attachments].[dbo].[Attachments]
SET Path = REPLACE(Path, CONCAT('-',[AttachmentID]), '')
WHERE [Path] LIKE CONCAT ('%','-',[AttachmentID],'-','%')

Since the additional ID is always prefixed by a hyphen, this seems to have worked. 由于附加ID始终以连字符作为前缀,因此这似乎可行。

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

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