Other: Trimming Field Lengths

Other: Trimming Field Lengths

Sometimes you may want to manually trim records so that they have certain lengths.

The scripts below provide examples on how to best do this.

  1. --This is the max length you want
  2. DECLARE @MaxLength INT = 50
  3.  
  4. --This is the characters you want for an ellipsis.
  5. --Notice that the default value is the ellipsis character
  6. --and not three periods. (File systems do not like trailing periods).
  7. DECLARE @Ellipsis NVARCHAR(MAX) = '…'
  8.  
  9. --We do math so that if we ellipsize, we still wont be over the max length.
  10. DECLARE @TrimTill INT = @MaxLength - LEN(@Ellipsis)
  11.  
  12. --Trim Contacts Full Name
  13. UPDATE
  14.             __M_Contacts
  15. SET
  16.             Final_FullName = CONCAT(SUBSTRING(Final_FullName, 1, @TrimTill), @Ellipsis)
  17. WHERE 1=1
  18.             AND LEN(Final_FullName) > @MaxLength
  19.  
  20. --Trim Matter Description
  21. UPDATE
  22.             __M_Matters
  23. SET
  24.             Final_Description = CONCAT(SUBSTRING(Final_Description, 1, @TrimTill), @Ellipsis)
  25. WHERE 1=1
  26.             AND LEN(Final_Description) > @MaxLength