How can I remove newline character from a string

  • Hello,

    I might have a string which contains text, newline characters and spaces which is fine.

    And also, I might have a string which contains just newline characters or just spaces or both, which is not fine.

    I can remove spaces by using function TRIM(), but I am not sure how to remove newline characters from the string.

    I am thinking of using functions Pos() and Replace() in a loop to replace newline characters with nothing, but it is does not look like an altimate way to solve this problem.

    Is there any other simpler way to remove newline characters from a string or my choices are limited and I have to use Pos(0 and Replace() functions.

    Thank you

  • Try this

    DECLARE @STR VARCHAR(20)

    SELECT @STR = 'HELLO'+CHAR(10)+'HELLO'+CHAR(13)+'DDD FFF'

    PRINT @STR

    PRINT REPLACE(REPLACE(@STR,CHAR(10),''),CHAR(13),'')

     

    Ram

     

  • You may have defined the problem wrong. Did you mean to ask this, instead? :

    'How can I detect that a string has nothing but whitespace?'

    Replace space, newline and carriage-return characters with null string, and check if the result is a null string.

    SELECT @is_whitespace = CASE WHEN REPLACE(REPLACE(REPLACE(@str,' ',''),CHAR(10),''),CHAR(13),'') = '' THEN 1 ELSE 0 END

  • Thanks Ram,

    Your suggestion worked for my query of removing newline characters.

    Thanks once again.

    Kanchit

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply