Split Big Notes

Split Big Notes

Some destinations have a max character count for notes.  The script below will split notes that exceed that length into multiple notes. The subject of the note will be {original subject} (Part X of Y) and the description will be the first X number of characters for the first note, and the next X number of characters for the second note, etc.  The original notes will be marked with a status of Deleted so UM will not try to insert them. 

  1. --Split giant notes into smaller notes.
  2. DECLARE @targetLEN BIGINT = 250;

  3. UPDATE __M_Common_Notes SET Final_Status = 'Deleted', Comment = 'Split to multiple notes' WHERE 1=1 AND LEN( Final_Description) > @targetLEN; WITH splitUp AS ( SELECT Id, Final_Parent_Type, Final_Parent_Id, Final_Date, Final_Subject, LEFT( Final_Description, @targetLEN ) notePart, RIGHT( Final_Description, LEN( Final_Description ) - @targetLEN ) remainingNote, 1 partNumber FROM __M_Common_Notes WHERE 1=1 AND LEN( Final_Description ) > @targetLEN

  4. UNION ALL

  5. SELECT Id, Final_Parent_Type, Final_Parent_Id, Final_Date, Final_Subject, LEFT (remainingNote, @targetLEN) notePart, IIF(LEN (remainingNote)<=@targetLEN, '', RIGHT (remainingNote, LEN(remainingNote) - @targetLEN)) remainingNote,
  6. s.partNumber + 1 partNumber
  7. FROM
  8. splitUp s
  9. WHERE 1=1
  10. AND LEN(remainingNote) >0 ) INSERT INTO __M_Common_Notes ( Id,
  11. Final_Parent_Type,
  12. Final_Parent_Id,
  13. Final_Date,
  14. Final_Subject,
  15. Final_Description,
  16. Final_DisplayOrder ) SELECT CONCAT(Id, ' --- PART ', partNumber) Id, Final_Parent_Type, Final_Parent_Id, Final_Date, CONCAT( Final_Subject, ' (', partNumber, ' of ', COUNT(*) OVER (PARTITION BY Id), ')') Final_Subject, notePart, partNumber FROM splitUp

  17. GO