Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2002
    Posts
    1

    Unanswered: 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

  2. #2
    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 ?

  3. #3
    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.



    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

  4. #4
    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

  5. #5
    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

  6. #6
    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

  7. #7
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •