I have a field in my database that I plan to truncate, for reporting purposes, to 50 characters with the statement below.
SELECT (CASE WHEN (LEN(Notes) > 50) THEN SUBSTRING(Notes, 0, 50) + '...' WHEN (LEN(Notes) < 50) THEN SUBSTRING(Notes, 0, LEN(Notes)) + '...' ELSE 'NO NOTES WERE ENTERED' END) AS Notes FROM MyTable
This works great, however, I would like to complete the last word in the notes field so that a word isn't cut off so I would like to use CHARINDEX, SUBSTRING, REVERSE and possibly the RIGHT function to return the last complete word that is part of a string roughly 50 characters long.
I have experimented and I'm not having much luck.
Any help would be appreciated.