I have read numerous posts about how to do exactly this and I can not get this to work. I have a table with a nvarchar(max)
column that can potentially contain dozens of pages worth of text. I need to split this into separate rows where each carriage return is entered (to split out the paragraphs).
I found the below function:
CREATE FUNCTION [dbo].[udf-Str-Parse]
(@String varchar(max), @Delimiter varchar(10))
Returns Table
As
Return
(Select
RetSeq = Row_Number() over (Order By (Select null)),
RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From
(Select x = Cast('<x>'+ Replace(@String, @Delimiter, '</x><x>') + '</x>' as xml).query('.')) as A
Cross Apply
x.nodes('x') AS B(i)
);
I have tested this on a simple dummy table using spaces as a delimiter and it works, but when I try this on my actual table with text using the following code query;
Select
A.AssignmentID, Notes = B.RetVal
From
dbo.Assignments A
Cross Apply
[dbo].[udf-Str-Parse](A.TitleNotes, char(13)) B
Where
AssignmentID = 20
I get the following message:
Msg 9411, Level 16, State 1, Line 1
XML parsing: line 2, character 127, semicolon expected
I have tried using different delimiters other than char(13)
, including spaces like with my simple table example, but I just get errors expecting semicolons.
Does anyone see what I am doing wrong?