【问题标题】:Update with wildcard使用通配符更新
【发布时间】:2019-08-29 08:33:48
【问题描述】:

需要更新所有具有子字符串的行,如下所示: 'forcestartpage=xx' 并将其替换为 'forcestartpage=18' * 子串前后有很多不应该改变的字符

试过了,不行:

update t_reminderscont
set body = REPLACE (body,'forcestartpage=__','forcestartpage=18')

谢谢

【问题讨论】:

  • “forcestartpage=XX”能否在您的值上重复多次?
  • 通配符只能与LIKEPATINDEX等模式搜索运算符/函数一起使用。 '_' 中的 REPLACE 将仅被视为下划线字符,而不是单个字符通配符。
  • 一些示例值也将对我们有很大帮助。我假设 forcestartpage 的值不能大于 99,如果它小于 10,它将有一个前导 0(即 'forcestartpage=08'
  • 不,只有一次(forcestartpage=XX)

标签: sql-server replace wildcard


【解决方案1】:

您可以使用STUFF 函数:

SELECT
    T.body,
    Replaced = STUFF(
        T.Body,                                 -- Insert in T.Body
        CHARINDEX('forcestartpage=', T.Body),   -- ... at the position where 'forcestartpage=' starts
        LEN('forcestartpage=18'),               -- ... while replacing 17 characters
        'forcestartpage=18')                    -- ... the value forcestartpage=18
FROM
    YourTable AS T
WHERE
    T.body LIKE '%forcestartpage=__%' AND
    T.body NOT LIKE '%forcestartpage=18%'

但是,这仅适用于 forcestartpage= 在每一行的首次出现。

【讨论】:

    猜你喜欢
    • 2018-05-24
    • 2018-05-22
    • 1970-01-01
    • 2023-04-04
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多