MySQL 替换出现在另一个字符串之后的字符串部分

标签 mysql

我有一个包含注释的 varchar 字段。每个注释项都位于单独的行上。下面是一个示例。 我正在尝试编写一个查询,用新日期替换字符串“Next Contact:”后面出现的日期时间变量。

正如您所看到的,注释中还有其他日期,因此仅使用 ReqExp 来搜索日期对我来说不起作用。我试图弄清楚如何使用 RegExp 来替换“Next Contact”之后出现的任何内容到行尾。

这是我尝试过的:

update funnel_deals set `note` = replace(substr(note,LOCATE('Next Contact:', `note`),35), 'Next Contact: 2014-12-12 11:15:00') where username = 'jfoster'

    update funnel_deals set `note` = replace(RIGHT(`note`, LOCATE('Next Contact:', `note`),35),'Next Contact: 2014-13-12 12:15:00') where username = 'jfoster'

Both return a syntax error:

The first query: [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') where username = 'jfoster'' at line 1

The second: [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '35),'Next Contact: 2014-13-12 12:15:00') where username = 'jfoster'' at line 1

以下是注释栏包含的内容:

Notes: aaa

Deal Name: Second V27 Quote
Contact Type: Demo-Set-Pres
Contact Disposition: 
Funnel Status: Presentation (Demo or Pitch)
Last Contact: 2014-10-08 10:25:30
Next Contact: 2014-10-12 10:15:00
Quote Amount: 1200
Deal Chances: 0

无论我如何更改查询,都会返回语法错误。这是解决这个问题的正确方法吗?

最佳答案

如果日期/时间格式是固定的,你可以尝试这样的事情

UPDATE funnel_deals
   SET note = CONCAT(SUBSTRING_INDEX(note, 'Next Contact: ', 1),
       'Next Contact: 2014-12-12 11:15:00',
       SUBSTRING(SUBSTRING_INDEX(note, 'Next Contact: ', -1), 20))
 WHERE username = 'foster';

这里是SQLFiddle 演示

关于MySQL 替换出现在另一个字符串之后的字符串部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26262488/

相关文章:

mysql - 使用 SQL 查询生成多个最大值和最小值

php - 将桶中的项目(或具有属性)均匀分布到属性后的另一个桶(或数组)

PHP/MySQL : Hashing common data

php - 使用 dbForge 插入查询未插入默认值

c# - 如何对 MySQL 查询进行排队,使它们顺序进行而不是并发,并防止过度使用 I/O?

php mysql 使用ip地址进行暴力保护

php - 使用输入数组的排序顺序对 mysql 的输出进行排序

MySQL - 两个表,比较两行

mysql - 优化与数据库的工作

mysql - 加入同一列的 SQL 表