簡體   English   中英

MySQL替換另一個字符串之后出現的字符串部分

[英]MySQL Replace portion of a string that appears after another string

我有一個包含注釋的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列包含的內容:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM