这似乎是做你以后 - 希望它的使用。我确信有更好的方法来做到这一点:)
为了完整性我在一个临时表中创建了一个两行的测试用例,因为我认为你有很多行需要处理。
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