Skip to content

Instantly share code, notes, and snippets.

@kevindb
Created July 6, 2016 18:55
Show Gist options
  • Save kevindb/ec61ae3c8b481ef1a1a5481448216af6 to your computer and use it in GitHub Desktop.
Save kevindb/ec61ae3c8b481ef1a1a5481448216af6 to your computer and use it in GitHub Desktop.
SQL Server function to truncate a string and add an ellipsis
CREATE FUNCTION [dbo].[f_truncateWithEllipsis]
(
@value VARCHAR(8000),
@maxLength INT
)
RETURNS VARCHAR(8000)
AS
BEGIN
SET @value = LTRIM(RTRIM(@value));
RETURN CASE WHEN LEN(@value) > @maxLength
THEN LEFT(@value, @maxLength - 1) + '…'
ELSE @value
END
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment