gpt4 book ai didi

sql - 如何在没有字符串最后一部分的情况下更新

转载 作者:行者123 更新时间:2023-12-04 21:20:30 26 4
gpt4 key购买 nike

考虑这个字符串:

http://kliks.affiliate4you.nl/?adv=17847&web=1426&subid=4083&url=http%3A%2F%2Fwww.bartsmit.com%2Fshop%2Fnl%2Fbsnl%2Fpaw-patrol%2Fpaw-patrol -racers-pup-zuma**%3Fchannel_code%3D83%26product_code%3D94193039%26referer%3Da4you&linkinfo=czA4YBartSmit**

  • 我在数据库字段中有数千个这样的字符串。
  • 我想删除字符串的粗体部分。
  • 我知道它以 %3Fchannel_code 开头
  • 我知道它以 czA4YBartSmit 结尾
  • 在这两者之间,每条记录都不同

我想做类似的事情:update table set string = (string, but without the part starting with %3Fchannel_code and ending with czA4YBartSmit)

最佳答案

您将能够使用 CHARINDEX 的组合更新表中的行定位开始位置,LEFT 截断字符串。

您可以使用“3Fchannel_code”和“czA4YBartSmit”作为过滤条件,将更新限制为仅包含并分别以这些术语结尾的字符串。此外,% 在与 LIKE 一起使用时用作通配符,因此需要使用 [%] 进行转义。

BEGIN TRAN;

UPDATE MyTable
SET [string] =
LEFT([string], charindex('%3Fchannel_code', [string]) - 1)
FROM MyTable
WHERE [string] like '%[%]3Fchannel_code%czA4YBartSmit'
-- Check the update with a SELECT

-- If you are Happy, COMMIT, otherwise ROLLBACK

SqlFiddle here

MSDN here 上很好地概述了使用 LEFT、RIGHT 和 CHARINDEX 进行字符串操作。

警告:以这种方式更新数据之前,建议您将更新包装在事务中,以防万一。

关于sql - 如何在没有字符串最后一部分的情况下更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29445473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com