這似乎是做你以後 - 希望它的使用。我確信有更好的方法來做到這一點:)
爲了完整性我在一個臨時表中創建了一個兩行的測試用例,因爲我認爲你有很多行需要處理。
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)
這篇文章是否幫助回答您的問題? http://stackoverflow.com/questions/2647/how-do-i-split-a-string-so-i-can-access-item-x – Daniel
謝謝丹尼爾。我試過了,但它沒有將字符串拆分到我想要的位置。 – user2536008
我不清楚它。是這樣的:你有一個領域包括所有文本,並且你想把它分成上面表格中的4個領域?請澄清,原來的領域究竟是什麼。 – FLICKER