TEXT columns? Yeah, that gets very icky very fast. Here is a really vague stab at the principal idea:
If Object_ID('tempdb..#Test') Is Not Null Drop Table #Test
Create Table #Test
(
EntryID Int,
SomeData VarChar(20),
UserName Char(12),
LongNotes Text
)
Declare @BigText VarChar(Max)
Set @BigText = 'The quick brown fox jumps over the lazy dog. '
Set @BigText = REPLICATE(@BigText, 3000)
Declare @BigText2 VarChar(Max)
Set @BigText2 = 'It was a bright cold day in April, and the clocks were striking thirteen. '
Set @BigText2 = REPLICATE(@BigText2, 7000)
Insert Into #Test
Select 1, 'Something', 'Andy', ''
Union All Select 2, 'Something Else', 'Bob', ''
Union All Select 3, 'No Notes', 'Frank', ''
-- Put something in the TEXT columns
Declare @pointer Binary(16)
Select @pointer = TEXTPTR(LongNotes) From #Test Where EntryID = 1
WriteText #Test.LongNotes @Pointer @BigText
Select @pointer = TEXTPTR(LongNotes) From #Test Where EntryID = 2
WriteText #Test.LongNotes @Pointer @BigText2
;
With Lengths As
(
Select EntryID, DATALENGTH(longnotes) TotalLength from #Test
),
Chunks As
(
Select EntryID, 0 As ChunkStart, 7999 As ChunkEnd, TotalLength, 0 As ChunkNumber, 0 As ChunkOrder From Lengths
Union All
Select EntryID, NewChunkStart, NewChunkEnd, TotalLength, NewChunkStart / 32000, NewChunkStart % 32000 / 8000 From Chunks
Cross Apply
(
Select ChunkStart + 8000 As NewChunkStart, ChunkEnd + 8000 As NewChunkEnd
) X
Where ChunkStart + 8000 < TotalLength
),
ChunkText As
(
Select T.EntryId, C.ChunkStart, SubString(T.LongNotes, C.ChunkStart, C.ChunkEnd) As ChunkText, ChunkNumber, ChunkOrder
From #Test T
Join Chunks C On T.EntryID = C.EntryID
)
Select
T.EntryID,
T.SomeData,
T.UserName,
ROW_NUMBER() Over (Partition By T.EntryID Order By C1.ChunkStart) As Ordinal,
Concat(C1.ChunkText, C2.ChunkText, C3.ChunkText, C4.ChunkText) As Text32000
From #Test T
Left Join ChunkText C1 On C1.EntryID = T.EntryID And C1.ChunkOrder = 1
Left Join ChunkText C2 On C1.EntryID = C2.EntryID And C1.ChunkNumber = C2.ChunkNumber And C2.ChunkOrder = 2
Left Join ChunkText C3 On C1.EntryID = C3.EntryID And C1.ChunkNumber = C3.ChunkNumber And C2.ChunkOrder = 3
Left Join ChunkText C4 On C1.EntryID = C4.EntryID And C1.ChunkNumber = C4.ChunkNumber And C2.ChunkOrder = 4
Note that due to the joys of TEXT you have to jump through some hoops not to avoid having things get truncated when they go over 8000 bytes. I also haven't added the username into the string, I'm not sure there is a cleaner way to do it other than either:
a) Reconstruct the entire TEXT column into a VarChar(Max), add it, then split that up into chunks.
b) Split it to slightly less than 8000 byte chunks so that there will be "spare capacity" in the 32000 characters to squeeze in the username at the end without having to worry about it overflowing.
I think I've stayed "2005 clean" with all my SQL, but I've only got a 2017 install to test it on so I can't entirely swear to it.