Skip to content

Instantly share code, notes, and snippets.

@kwestground
Created December 9, 2022 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwestground/7db6384e42d05123d16ecc425a962f61 to your computer and use it in GitHub Desktop.
Save kwestground/7db6384e42d05123d16ecc425a962f61 to your computer and use it in GitHub Desktop.
Truncate string and count items from comma
CREATE OR ALTER FUNCTION dbo.func_TruncateWithCount
(
@input nvarchar(max), @MaxLength int
)
RETURNS nvarchar(max)
AS
BEGIN
IF (LEN(@input) <= @MaxLength)
RETURN @input
DECLARE @CountItems int;
SET @CountItems = LEN(@input) - LEN(REPLACE(@input, ',', '')) + 1;
RETURN SUBSTRING(@input, 1, @MaxLength) + '...('+CAST(@CountItems AS varchar)+')';
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment