2014-10-08 125 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

相關問題