2014-10-08 124 views
0

我有一个包含注释的varchar字段。每个笔记项目都在单独的行中。下面显示一个例子。 我正在尝试编写一个查询来替换出现在字符串“Next Contact:”后面的日期时间var与新日期。MySQL替换出现在另一个字符串后面的字符串部分

正如您所看到的,笔记中还有其他日期,因此只需要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 

不管我如何更改查询,语法错误返回。这甚至是解决这个问题的正确方法吗?

+0

为什么不能在应用层这样做呢?更好的是,为什么不将与这些注释相关的信息存储到单独的数据库字段?这似乎是管理这些数据的一种非常笨拙的方式。 – 2014-10-08 17:25:54

+0

这是别人的应用程序。我们提供日程安排更改并需要更新其系统。我坚持它是什么。 – 2014-10-08 17:27:36

回答

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

+0

谢谢@peterm。像魔术一样工作。 – 2014-10-08 20:58:09

+0

@Len_D你很受欢迎。我很高兴它有帮助。 – peterm 2014-10-09 14:22:08

相关问题