Shorten a SQL VarChar result.

K

Kenneth Keeley

Hi,
I am creating a news and events page for my web site and I have a data
grid for the summary of news items that is using a TemplateColumn I can
layout the data in the format I wish. My only problem is that one of the
fields returned by the database is a very long VarChar, It is the story
field and thus can be hunderds of characters long. I wish to cut this back
to about 100 characters and then provide a link to a page that displays the
full story. My question is how do I dsiplay only the first 100 characters of
the story.

Thanks
Kenneth
 
S

Scott Mitchell [MVP]

Kenneth said:
I am creating a news and events page for my web site and I have a data
grid for the summary of news items that is using a TemplateColumn I can
layout the data in the format I wish. My only problem is that one of the
fields returned by the database is a very long VarChar, It is the story
field and thus can be hunderds of characters long. I wish to cut this back
to about 100 characters and then provide a link to a page that displays the
full story. My question is how do I dsiplay only the first 100 characters of
the story.

Kenneth, there are a couple of ways:

(1) Truncate the length on the SQL side. That is, use something like
the SUBSTRING SQL Server function (see http://tinyurl.com/579g5) to
truncate the varchar field.

(2) Bring back the entire SQL string, and then truncate it in your
ASP.NET page. If you are using a DataGrid you can use this free, custom
DataGrid column, LimitColumn -
http://aspnet.4guysfromrolla.com/articles/100202-1.aspx. Or you could
use a TemplateColumn along with a "helper function" that would receive
the long field as input and use the .Substring() method to return a
shortened version of the string.

hth

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top