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

时间:2014-10-08 17:05:07

标签: mysql

我有一个包含备注的varchar字段。每个注释项都在一个单独的行上。下面是一个例子。 我正在尝试编写一个查询来替换字符串" Next Contact:"之后出现的datetime var。新约会。

正如您所看到的,笔记中还有其他日期,因此单独搜索日期的简单ReqExp对我来说不起作用。我试图弄清楚如何使用RegExp来替换在“下一个联系人”之后出现的内容。到最后一行。

以下是我的尝试:

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

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

1 个答案:

答案 0 :(得分:1)

如果日期/时间格式已修复,您可以尝试这样的

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 演示