Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Finding a certain character

Finding a certain character

Scheduled Pinned Locked Moved Database
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Ballita
    wrote on last edited by
    #1

    Hi all, I have different characters in a string, My string may contain CHAR(10) or CHAR(13) + CHAR(10). If my string contain only CHAR(10) then I want to replace it with CHAR(13) + CHAR(10). And if my string contain CHAR(13) + CHAR(10) then I don't want to replace it. Can any body give me a solution to this.

    Thanks & Regards Mishra

    M S 2 Replies Last reply
    0
    • B Ballita

      Hi all, I have different characters in a string, My string may contain CHAR(10) or CHAR(13) + CHAR(10). If my string contain only CHAR(10) then I want to replace it with CHAR(13) + CHAR(10). And if my string contain CHAR(13) + CHAR(10) then I don't want to replace it. Can any body give me a solution to this.

      Thanks & Regards Mishra

      M Offline
      M Offline
      Manas Bhardwaj
      wrote on last edited by
      #2

      Find the characters in string like this...

      DECLARE @position int, @string char(8)
      -- Initialize the current position and the string variables.
      SET @position = 1
      SET @string = 'New Moon'
      WHILE @position <= DATALENGTH(@string)
      BEGIN
      SELECT ASCII(SUBSTRING(@string, @position, 1)),
      CHAR(ASCII(SUBSTRING(@string, @position, 1)))
      SET @position = @position + 1
      END
      GO

      You may modify/build a new string upon your requirements...

      Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      1 Reply Last reply
      0
      • B Ballita

        Hi all, I have different characters in a string, My string may contain CHAR(10) or CHAR(13) + CHAR(10). If my string contain only CHAR(10) then I want to replace it with CHAR(13) + CHAR(10). And if my string contain CHAR(13) + CHAR(10) then I don't want to replace it. Can any body give me a solution to this.

        Thanks & Regards Mishra

        S Offline
        S Offline
        Syed Mehroz Alam
        wrote on last edited by
        #3

        We can do this in two cascaded steps: 1. Replace all occurrences of Char(10) with Char(13)+Char(10) 2. Replace all occurrences of Char(13)+Char(13)+Char(10) with Char(13)+Char(10) Note that Step 1 replaces some unwanted occurrences also (i.e. Char(13)+Char(10) occurrences), but the second step fixes those unwanted wrong replaces. Have fun with this piece of code:

        declare @myTestString varchar(50)

        set @myTestString = char(10)+char(13)+char(10)+char(10)+char(13)+char(13)+char(13)+char(10)

        set @myTestString = replace( @myTestString, char(10), char(13)+char(10) )
        set @myTestString = replace( @myTestString, char(13)+char(13)+char(10), char(13)+char(10) )

        Cheers, Syed Mehroz Alam

        My Blog My Articles

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups