Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limiting Characters in Data String

Status
Not open for further replies.

GavW

Programmer
Jan 13, 2006
58
GB
Hey All!

I was wondering what the best way to acheive this result would be. I have a 'Description' field within my database table that I would like to shorten on one of the pages, like so:

This is a really really long description

would become:

This is a really rea...

I have tried using the following SQL queries:

SELECT ID, Name, LEFT(Description, 40) + '...' AS 'ShortDesc'
FROM tbl

SELECT ID, Name, SUBSTRING(Description, 1, 40) + '...' AS Description
FROM tbl

None of these give me the desired results. Is there a way that I can manipulate this data through the code rather than within the SQL query? or am I just getting the SQL syntax wrong? I am coding in C#

Thanks
 
Are you using SQL Server? Your SUBSTRING function should work. Maybe you can try naming it something else, to differentiate it from the original field name?

SUBSTRING(Description, 1, 40) AS MyDescription
 
just tried that still no luck.

It is recognising the fact that I have a new column "MyDescription" but instead of returning the first 40 characters it isn't returning anything, just a blank string.
 
Those should work. what do you mean you don't get the desired results? It doesn't get much easier than using LEFT()

Jim
 
well the data is not outputting how I would like it on the page (This is a really rea...)

Instead I am receiving no data at all, just an empty string.

When telling the application to output 'Description' I receive the entire string and when telling the application to output 'MyDescription' I am receiving nothing.
 
Can you post the code of your page where you are trying to output the data?
 
Sure thing. I am using a repeater to output the information. To save confusion I will edit the code down a bit. The line where the description is output I have separated so it is easier to see. I have tried 'MyDescription' in the place of 'Description'.

<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<table><tr><td width="110px">
<a href="productdetail.aspx?ProductID=<%# DataBinder.Eval ( Container.DataItem, "ProductID" ) %>">
<img align="absmiddle" alt="Product" border="0" height="80" src='<%# DataBinder.Eval ( Container.DataItem, "Image" ) %>' width="100"></a>
</td><td width="180px">
<a href="productdetail.aspx?ProductID=<%# DataBinder.Eval ( Container.DataItem, "ProductID" ) %>">
<span style="text-decoration: underline;"><font color=blue size="3px">
<%# DataBinder.Eval ( Container.DataItem, "Name" ) %></font></span></a>
<br /><font face="Verdana" size="2px">

<%# DataBinder.Eval ( Container.DataItem, "Description" ) %></font>

</td></tr></table>
</ItemTemplate>
</asp:Repeater>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top