Results 1 to 13 of 13

Thread: [RESOLVED] Trim a carriage return from end of string

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Trim a carriage return from end of string

    I need to alter a select statement so that a returned field does not have an unwanted carriage return at the end.

    I think the code below would remove all the carriage returns but I only want to remove one if it appears at the very end of the string.

    SELECT REPLACE([parameters],Char(10),'') as [parameters]
    FROM QUERIES

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Trim a carriage return from end of string

    Which database system(s) are you using?

  3. #3

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Trim a carriage return from end of string

    Sorry it is SQL Server 2010.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Trim a carriage return from end of string

    If it is just Char(10) you are interested in, something like this should do it:
    Code:
    SELECT CASE WHEN Right([parameters],1) = Char(10) 
                THEN Left([parameters], Length([parameters])-1)
                ELSE [parameters]
                END as [parameters]
    FROM QUERIES
    If you want to allow a mixture of Char(10) and Char(13) it gets a bit more awkward, and it would probably be better to create a function.

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Trim a carriage return from end of string

    I did not know there was a SQL Server 2010. Where did you find that. I thought the last relase was SQL Server 2008R2 and the SQL Server "Denali" CTP1 was the latest preview.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Trim a carriage return from end of string

    (not tested code)
    sql Code:
    1. CREATE FUNCTION RTrimChar(@Input VARCHAR(MAX), @TrimChar CHAR(1))
    2. RETURNS VARCHAR(MAX)
    3. AS
    4. BEGIN
    5.     WHILE RIGHT(@Input, 1) = @TrimChar
    6.     SET @Input = LEFT(@Input, LEN(@Input)-1)
    7.     RETURN @Input
    8. END
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Trim a carriage return from end of string

    Quote Originally Posted by si_the_geek View Post
    If it is just Char(10) you are interested in, something like this should do it:
    Code:
    SELECT CASE WHEN Right([parameters],1) = Char(10) 
                THEN Left([parameters], Length([parameters])-1)
                ELSE [parameters]
                END as [parameters]
    FROM QUERIES
    If you want to allow a mixture of Char(10) and Char(13) it gets a bit more awkward, and it would probably be better to create a function.
    I In actual fact I do want to remove Char13 but I need to remove all Char13 from the string but only remove a Char10 if it is at the end of the string.

    REPLACE(CASE WHEN Right([parameters],1) = Char(10)
    THEN Left([parameters], Len([parameters])-1)
    ELSE [parameters]
    END,Char(13),'')
    as [parameters]
    Currently tried this but it still does not seem to remove the extra carriage return at the end of my string.

    If I copy and paste the output to word here is what it returns (hope you can figure out what I I mean by the improvisation of symbols at the end of each row.).

    Start Date : 07/12/2010<&#172;
    End Date : 08/12/2010<&#172;
    Include IT : Y<&#172;
    Report Names : Select All P|
    P|
    Last edited by FishGuy; Dec 8th, 2010 at 04:57 AM.

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Trim a carriage return from end of string

    Try adding Pradeep1210's function, then using this:
    Code:
    SELECT RTrimChar (REPLACE([parameters],Char(13),''), Char(10))
    ...

  9. #9

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Trim a carriage return from end of string

    I just tried that, is it possible that there are two CRs at the end of the string, how would the function handle that?

    Alternatively, is there another char that could be causing this i.e. not char10 or 13?
    Last edited by FishGuy; Dec 8th, 2010 at 05:30 AM.

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Trim a carriage return from end of string

    Just like RTrim function removes all trailing spaces, this function removes all trailing custom character you specify. Doesn't matter how many they are.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Trim a carriage return from end of string

    What it wont handle however is any other characters (such as spaces) between those characters, so it may be a good idea to add this:
    Code:
        SET @Input = RTrim(LEFT(@Input, LEN(@Input)-1))

  12. #12

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Trim a carriage return from end of string

    Still not working, is there any way to get the actual char output from the data to see what is going there.

  13. #13

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Trim a carriage return from end of string

    Thanks guys, that now works.

Posting Permissions

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



Click Here to Expand Forum to Full Width