2014-10-17 29 views
0

我有評論表,其中有CommentText提交。現在我想刪除這個[quote]和[/ quote]中的文本。在這種情況下,我可以通過刪除引用文本來減少表格的大小並保留主要評論。另外一些時候,我們有嵌套報價這將是這樣的:SQL查詢刪除整個表中的文本部分

**[quote name="sAe!D"] 
    some text here 
    [quote name="John"] 
     Another nested text here 

     [quote name="User2"] 
      Another nested text here 
     [/quote] 
    [/quote] 
[/quote] 
Here would be the Text of comment owner which should be Kept.** 

我需要除了刪除所有「這裏將是評論所有者應保持的文字。」

+1

您使用的是什麼RDMS? SQLServer,MySQL,Oracle或其他? – 2014-10-17 08:46:17

回答

0

嗨,歡迎來到StackOverflow。您可以嘗試在評論文本列上使用一些文本操作,刪除「[quote」後面的所有內容,並將其添加到反轉註釋文本並執行相同操作(在[[quote]之後移除所有內容),但向後移動)。這將只留下標籤外的位:

DECLARE @RemoveFrom VARCHAR(50) = '[quote' 
DECLARE @RemoveTo VARCHAR(50) = '[/quote]' 

-- Take all text before the text we remove 
SELECT LEFT(CommentText,CHARINDEX(@RemoveFrom,CommentText,1)-1) 
-- add all text after the text we remove (which is the reverse of above, with the result reversed) 
    + REVERSE(LEFT(REVERSE(ComentText),CHARINDEX(REVERSE(@RemoveTo),REVERSE(CommentText),1)-1)) 
FROM Comment_Table 
+0

謝謝你的回答,我會測試並讓你知道結果。 – user2083133 2014-10-18 09:55:33