Maciej Los
Решение 2 предоставлено компанией Мика Венделиус[^] очень хорошо. Я бы предложил использовать КТОС[^]:
DECLARE @tmp TABLE(MyText VARCHAR(500))
INSERT INTO @tmp (MyText)
VALUES('This is very strange text...'),
('This is another very strange text.'),
('This is very stupid text !!!')
DECLARE @words TABLE(RowNo INT, PartNo INT, MyWord VARCHAR(255))
;WITH CTE AS
(
SELECT ROW_NUMBER() OVER(ORDER BY MyText) AS RowNo, 1 AS PartNo, LEFT(LTRIM(MyText), CHARINDEX(' ', LTRIM(MyText))-1) AS MyWord, RIGHT(LTRIM(MyText), LEN(LTRIM(MyText)) - CHARINDEX(' ', LTRIM(MyText))) AS Remainder
FROM @tmp
WHERE CHARINDEX(' ', LTRIM(MyText))>0
UNION ALL
SELECT RowNo, PartNo + 1 AS PartNo, LEFT(LTRIM(Remainder), CHARINDEX(' ', LTRIM(Remainder))-1) AS MyWord, RIGHT(LTRIM(Remainder), LEN(LTRIM(Remainder)) - CHARINDEX(' ', LTRIM(Remainder))) AS Remainder
FROM CTE
WHERE CHARINDEX(' ', LTRIM(Remainder))>0
UNION ALL
SELECT RowNo, PartNo + 1 AS PartNo, LTRIM(Remainder) AS MyWord, NULL AS Remainder
FROM CTE
WHERE CHARINDEX(' ', LTRIM(Remainder))=0
)
INSERT INTO @words (RowNo, PartNo, MyWord)
SELECT RowNo, PartNo, RTRIM(LTRIM(MyWord)) AS MyWord
FROM CTE
ORDER BY RowNo, PartNo
--SELECT *
--FROM @words
--ORDER BY RowNo, PartNo
SELECT DISTINCT t2.RowNo,
(
SELECT t1.MyWord + ' ' AS [text()]
From @words AS t1
Where t1.RowNo = t2.RowNo
ORDER BY t1.PartNo
For XML PATH ('')
) AS MySentence
From @words AS t2
Результат:
RowNo MySentence
1 This is very strange text...
2 This is very stupid text !!!
3 This is another very strange text.