Categories:
Cloud (163)
Entertainment (11)
Facebook (43)
General (17)
Life (30)
Programming (33)
Technology (249)
Testing (4)
Tools (448)
Twitter (5)
Wearable (26)
Web Design (9)
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, 1161👍, 0💬
Popular Posts:
What is a slide master in PowerPoint? A slide master is a set of slide layouts defined to help you c...
What are header and footer design options in Microsoft Word? I want to learn more about creating hea...
How to add ActiveX controls, like text command button, text box, check box, etc., to Presentation sl...
Where does Mozilla Firefox 2 store download files? When you download files from Websites, Mozilla Fi...
What is the difference between a Web page and a Single File Web Page? Word supports 2 Web page forma...