Categories:
Cloud (204)
Entertainment (11)
Facebook (43)
General (50)
Life (31)
Programming (64)
Technology (430)
Testing (4)
Tools (488)
Twitter (5)
Wearable (26)
Web Design (44)
Collections:
Other Resources:
SQL Server Truncating Message with '...'
What is the best way in SQL Server to truncate a string to 140 characters and add "..." to the end if there are more than 140 characters in the string? I need this to display initial parts of user comments on my home page.
✍: FYIcenter
Basically, you are asking the logic to be implemented in a SQL Server query,
If the string has more than 140 characters, Return the first 140 characters with "..." added to the end Else Return the string as is
A straightforward solution is the following:
SELECT CASE WHEN LEN(input)>140 THEN SUBSTRING(input,1,140)+'...' ELSE input END;
But the following solution should have a better performance:
SELECT SUBSTRING(input,1,140) + REPLACE(REPLACE(CAST(
PATINDEX('%_%',SUBSTRING(input,140,999999))
AS NVARCHAR(MAX)),'0',''),'1','...');
Of course, when SQL Server supports Regular Expression, we will have a much better solution.
2015-09-16, 2320🔥, 0💬
Popular Posts:
How to edit Mozilla Firefox configuration file? Mozilla Firefox has a configuration file that contro...
How do I tell what version of Outlook my computer is using? You can determine the version number of ...
How to download and install older versions of Microsoft Office on my Mac computer? I am running macO...
What are the steps to insert Dynamic Fields in Microsoft Word? I want to insert the chapter heading ...
Where to find answers to frequently asked questions on installing and using Mozilla Firefox 2.0? Her...