2016-03-10 102 views
-1

我有一個名爲description的文本字段,列出了很多信息,並且我想提取特定單詞之間的字符串。文字值樣本:從文本字段中提取字符串SQL Server 2012

一般優點:這樣做的目的是解釋使用我們產品的好處。

健康因素:請查看我們的網站獲取完整列表。

評論:在這裏輸入用戶添加的任何評論。

聯繫我們:請致電XXX-XXX-XXXX

一般福利,健康因素,評論,並聯系我們將永遠在文本字段,但每一個後面的語句是不同的。 輸出應該提取這四個字符串之間的字符串: enter image description here

+0

這篇文章是否幫助回答您的問題? http://stackoverflow.com/questions/2647/how-do-i-split-a-string-so-i-can-access-item-x – Daniel

+0

謝謝丹尼爾。我試過了,但它沒有將字符串拆分到我想要的位置。 – user2536008

+0

我不清楚它。是這樣的:你有一個領域包括所有文本,並且你想把它分成上面表格中的4個領域?請澄清,原來的領域究竟是什麼。 – FLICKER

回答

1

這似乎是做你以後 - 希望它的使用。我確信有更好的方法來做到這一點:)

爲了完整性我在一個臨時表中創建了一個兩行的測試用例,因爲我認爲你有很多行需要處理。

CREATE TABLE #Test (Txt NVARCHAR(MAX)) 

INSERT INTO #Test (Txt) VALUES 
(N'General Benefits: The purpose of this is to explain the benefits from using our products. Health Factors: Please check out our website for a complete list. Comments: Any comment added by the user is entered here. Contact Us: Please call us at xxx-xxx-xxxx'), 
(N'General Benefits: Some other benefits. Health Factors: Some other factors. Comments: Love your work. Contact Us: Call us zzz-zzz-zzzz'); 


DECLARE @PAT1 NVARCHAR(MAX) = N'General Benefits:'; 
DECLARE @PAT2 NVARCHAR(MAX) = N'Health Factors:'; 
DECLARE @PAT3 NVARCHAR(MAX) = N'Comments:'; 
DECLARE @PAT4 NVARCHAR(MAX) = N'Contact Us:'; 



SELECT 
    SUBSTRING(Txt, A + LEN(@PAT1) + 1, B - A - LEN(@PAT1) - 1) 'General Benefits', 
    SUBSTRING(Txt, B + LEN(@PAT2) + 1, C - B - LEN(@PAT2) - 1) 'Health Factors', 
    SUBSTRING(Txt, C + LEN(@PAT3) + 1 , D - C - LEN(@PAT3) - 1) 'Comments', 
    SUBSTRING(Txt, D + LEN(@PAT4) + 1 , LEN(Txt) - LEN(@PAT4) - 1) 'Contact Us' 
FROM #Test 
CROSS APPLY (SELECT 
    A = PATINDEX('%'[email protected]+'%', Txt), 
    B = PATINDEX('%'[email protected]+'%',Txt), 
    C = PATINDEX('%'[email protected]+'%',Txt), 
    D = PATINDEX('%'[email protected]+'%',Txt) 
) Q 


General Benefits                                                             Health Factors                                                             Comments                                                               Contact Us 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
The purpose of this is to explain the benefits from using our products.                                               Please check out our website for a complete list.                                                    Any comment added by the user is entered here.                                                     Please call us at xxx-xxx-xxxx 
Some other benefits.                                                            Some other factors.                                                            Love your work.                                                             Call us zzz-zzz-zzzz 

(2 row(s) affected) 
+0

非常感謝!它效果很好。 – user2536008