Here is a cte which will normalize your data. From there you can pivot
Declare @Customer table (FirstName varchar(50),LastName varchar(50),DOB Date,Memo varchar(max))
Insert into @Customer values
('John','Doe' ,'1964-07-29','I''ve looked at about 15 different answers on SO but haven''t found this exact situation yet. I''m doing a custom data export and need to export to a data file that will be imported into an older system that needs the data in a specific length/format. I have a "MEMO" column that can have a large amount of text in it. I need to select that value and split it into multiple columns with a FIXED length of 75 chars. For instance, if I have a row with a message that is 185 chars, I need to split that into 3 new columns of 75 chars, MEMO1, MEMO2, MEMO3, with the remaining space in MEMO3 being filled with spaces to equal the 75 chars. The other catch, I can only use up to 18 75-char columns to dump the data into. If it''s longer than 1350 (18x75) chars, the rest gets truncated.'),
('Jane','Smith','1972-03-21','I''m a long-time application dev who is trying to get more involved in the DB side of things. If I were in the C# world, I would just create a method to do a for loop up to NUMBEROFMEMOS and output the data that way. I don''t think that works here though. Thanks in advance!')
Declare @MaxLen int = 75
;with cteBase as (
Select FirstName,LastName,DOB,Row=1,Memo=substring(Memo,1,@MaxLen) from @Customer
Union All
Select h.FirstName,h.LastName,h.DOB,Row=cteBase.Row+1,Memo=substring(h.Memo,((cteBase.Row+0)*@MaxLen)+1,@MaxLen) FROM @Customer h INNER JOIN cteBase ON h.FirstName = cteBase.FirstName and h.LastName = cteBase.LastName where substring(h.Memo,((cteBase.Row+0)*@MaxLen)+1,@MaxLen)<>''
)
--Select * from cteBase Order by LastName,Row
Select FirstName,LastName,DOB
,Memo01=max(case when Row=1 then Memo else null end)
,Memo02=max(case when Row=2 then Memo else null end)
,Memo03=max(case when Row=3 then Memo else null end)
,Memo04=max(case when Row=4 then Memo else null end)
,Memo05=max(case when Row=5 then Memo else null end)
,Memo06=max(case when Row=6 then Memo else null end)
,Memo07=max(case when Row=7 then Memo else null end)
,Memo08=max(case when Row=8 then Memo else null end)
,Memo09=max(case when Row=9 then Memo else null end)
,Memo10=max(case when Row=10 then Memo else null end)
,Memo11=max(case when Row=11 then Memo else null end)
,Memo12=max(case when Row=12 then Memo else null end)
,Memo13=max(case when Row=13 then Memo else null end)
,Memo14=max(case when Row=14 then Memo else null end)
,Memo15=max(case when Row=15 then Memo else null end)
,Memo16=max(case when Row=16 then Memo else null end)
,Memo17=max(case when Row=17 then Memo else null end)
,Memo18=max(case when Row=18 then Memo else null end)
from cteBase
Group By FirstName,LastName,DOB
Order by LastName
The CTE Returns
FirstName LastName DOB Row Memo
John Doe 1964-07-29 1 I've looked at about 15 different answers on SO but haven't found this exac
John Doe 1964-07-29 2 t situation yet. I'm doing a custom data export and need to export to a dat
John Doe 1964-07-29 3 a file that will be imported into an older system that needs the data in a
John Doe 1964-07-29 4 specific length/format. I have a "MEMO" column that can have a large amount
John Doe 1964-07-29 5 of text in it. I need to select that value and split it into multiple colu
John Doe 1964-07-29 6 mns with a FIXED length of 75 chars. For instance, if I have a row with a m
John Doe 1964-07-29 7 essage that is 185 chars, I need to split that into 3 new columns of 75 cha
John Doe 1964-07-29 8 rs, MEMO1, MEMO2, MEMO3, with the remaining space in MEMO3 being filled wit
John Doe 1964-07-29 9 h spaces to equal the 75 chars. The other catch, I can only use up to 18 75
John Doe 1964-07-29 10 -char columns to dump the data into. If it's longer than 1350 (18x75) chars
John Doe 1964-07-29 11 , the rest gets truncated.
Jane Smith 1972-03-21 1 I'm a long-time application dev who is trying to get more involved in the D
Jane Smith 1972-03-21 2 B side of things. If I were in the C# world, I would just create a method t
Jane Smith 1972-03-21 3 o do a for loop up to NUMBEROFMEMOS and output the data that way. I don't t
Jane Smith 1972-03-21 4 hink that works here though. Thanks in advance!