If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > How do I truncate Text in ASP

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-05-02, 10:07
bluejean bluejean is offline
Registered User
 
Join Date: Dec 2002
Posts: 1
How do I truncate Text in ASP

Hello there

I am using DWMX - and I was wondering if it would be big problem to truncate text from the database. Like displaying like only 32 first letters from the record.

regards

Jean Blahamar
Reply With Quote
  #2 (permalink)  
Old 12-06-02, 23:06
rnealejr rnealejr is offline
Registered User
 
Join Date: Feb 2002
Posts: 2,232
Which database are you using ? Do you mean to actually truncate the data in the database or only truncate the data returned to you ?
Reply With Quote
  #3 (permalink)  
Old 12-09-02, 22:53
hktang hktang is offline
Registered User
 
Join Date: Nov 2002
Location: Singapore
Posts: 17
Re: How do I truncate Text in ASP

Hi,

Correct me if I understand you wrong.

So you would like to show only first 32 characters of the record in the database?

-> SQL (when you quering the database).
The syntax is :- SUBSTRING(expression, start, length)

For example:
select substring(columnName, 1, 32) as myColumnName from tableName

This will then return the result with 32 characters.

-- or --

-> ASP
myResult = MID(recordset("columnName"), 1, 32)

The record result will be assigned to the variable myResult.


Hope this solve your query.



Quote:
Originally posted by bluejean
Hello there

I am using DWMX - and I was wondering if it would be big problem to truncate text from the database. Like displaying like only 32 first letters from the record.

regards

Jean Blahamar
Reply With Quote
  #4 (permalink)  
Old 12-10-02, 11:00
JonathanB JonathanB is offline
Registered User
 
Join Date: Feb 2002
Location: North Wales, UK
Posts: 114
You can also use either the VBScript functions left(<string>, <length>) or right(<string>, <length>).
__________________
J^ - web | email
newsASP Developer
Reply With Quote
  #5 (permalink)  
Old 12-10-02, 11:38
Mulligan Mulligan is offline
Registered User
 
Join Date: Jul 2002
Posts: 55
Also, bear in mind these VB* functions will throw an error if the length of the string is less than 32 characters, so:
Code:
If Len(myString) >= 32 Then
   myString = Left(myString, 32)
End If
Is a defensive way of coding the statement. I always get nailed by that one
Reply With Quote
  #6 (permalink)  
Old 12-16-02, 05:03
JonathanB JonathanB is offline
Registered User
 
Join Date: Feb 2002
Location: North Wales, UK
Posts: 114
What version of ASP/IIS/Script Engine are you running as I've never had a problem using left() or right() on strings with less than 32 characters??
__________________
J^ - web | email
newsASP Developer
Reply With Quote
  #7 (permalink)  
Old 02-24-04, 03:20
teh1337ASPer teh1337ASPer is offline
Registered User
 
Join Date: Feb 2004
Posts: 1
for a super l337 version of truncating you can use the following code:

Code:
<%
Dim intTruncateat, txtMessage, lLen, lCtr, sChar, sAns, intSpaceAt

intTruncateAt = 32
txtMessage = "This will find the final space in this sentence automatically"

if len(txtMessage) > intTruncateAt Then
txtMessage = left(txtMessage, intTruncateAt)

lLen = Len(txtMessage)
For lCtr = lLen To 1 Step -1
    sChar = Mid(txtMessage, lCtr, 1)
    sAns = sAns & sChar
Next
intSpaceAt = instr(sAns, " ")
intSpaceAt = intTruncateAt - intSpaceAt
txtMessage = left(txtMessage, intSpaceAt)
response.write txtMessage & "..."

else
response.write txtMessage
end if
%>
That'll cut the last debris text, in case there is harmful stuff such as:

Code:
<br
Which leaves open a tag and can harm the rest of your code.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On