Replacing repeating blanks with a single blank

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • leela mn
    New Member
    • May 2007
    • 43

    Replacing repeating blanks with a single blank

    hi all,

    I have a scenario where in i need to replace repeating blanks with a single blank of a column in the database.

    ex: ASD KL OP ---> ASD KL OP

    any suggestions??

    Regards,
    Leela
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Originally posted by leela mn
    hi all,

    I have a scenario where in i need to replace repeating blanks with a single blank of a column in the database.

    ex: ASD KL OP ---> ASD KL OP

    any suggestions??

    Regards,
    Leela

    the challenge: you don't know at any point in time how many spaces are there in between. this is string cleaning (like address), you might want to create a function that parse the string and remove repeating blanks. consideration: not because you can not see does not mean it's blank. there are "unprintabl e" characters.

    -- CK

    Comment

    • leela mn
      New Member
      • May 2007
      • 43

      #3
      Originally posted by ck9663
      the challenge: you don't know at any point in time how many spaces are there in between. this is string cleaning (like address), you might want to create a function that parse the string and remove repeating blanks. consideration: not because you can not see does not mean it's blank. there are "unprintabl e" characters.

      -- CK
      but in my case i am sure that its a space but is there any one liner function which can do this trick?????

      Comment

      • ck9663
        Recognized Expert Specialist
        • Jun 2007
        • 2878

        #4
        Originally posted by leela mn
        but in my case i am sure that its a space but is there any one liner function which can do this trick?????
        is this a one time project? how many fields are you trying to clean up?

        -- CK

        Comment

        • leela mn
          New Member
          • May 2007
          • 43

          #5
          Originally posted by ck9663
          is this a one time project? how many fields are you trying to clean up?

          -- CK
          its not a project or something, there is one column in the database which has spaces in between and needs to be removed.

          Comment

          • ck9663
            Recognized Expert Specialist
            • Jun 2007
            • 2878

            #6
            Originally posted by leela mn
            its not a project or something, there is one column in the database which has spaces in between and needs to be removed.

            if it's a one-time, manual kind of task try:

            Code:
            update yourtable
            set yourfield = replace(replace(replace(yourfield,'  ',' '),'  ',' '),'  ',' ')
            where patindex('%  %',yourfield) > 0
            you're going to have to run this a number of times, until it no longer returns any resultset. it will just replace any 2 consecutive space with 1 space. so, if there are 3 spaces in between string, it will be reduced to 2, but the second replace will reduce it further to 1. if there are 4 it will reduce to 2, and so on...

            you can try this first:

            Code:
            select yourfield, replace(replace(replace(yourfield,'  ',' '),'  ',' '),'  ',' ')
            from yourtable
            where patindex('%  %',yourfield) > 0

            this will give you an idea if you're in the right direction

            -- CK

            Comment

            • leela mn
              New Member
              • May 2007
              • 43

              #7
              thanks for ur info... :)
              this i have tried i wanted some other better approach than this if any........

              Comment

              • ck9663
                Recognized Expert Specialist
                • Jun 2007
                • 2878

                #8
                create a string parser function...


                -- CK

                Comment

                • hailua
                  New Member
                  • Jan 2008
                  • 4

                  #9
                  Create FUNCTION [dbo].[replace_blank]
                  ( @input nvarchar(max))
                  RETURNS nvarchar(4000) As
                  Begin
                  Declare @output nvarchar(4000)
                  SET @input = ltrim(rtrim(@in put))
                  SET @output = ''

                  While 1=1
                  Begin
                  IF PATINDEX('% %',@input) = 0 BREAK
                  SET @output = ltrim(rtrim(@ou tput))+' '+ltrim(rtrim(S ubString(@input ,1,CharIndex(' ',@input))))
                  SET @input = SubString(@inpu t,CharIndex(' ',@input)+1,Len (@input))
                  End
                  SET @output = Left(@output,Le n(@output))+' '+@input
                  Return @output
                  End

                  -- select dbo.replace_bla nk('hello: is this the one you looking at?. ')

                  Comment

                  • hailua
                    New Member
                    • Jan 2008
                    • 4

                    #10
                    Or you can use
                    -----------------------------
                    Create FUNCTION [dbo].[replace_blank]
                    ( @input nvarchar(4000))
                    RETURNS nvarchar(4000) As
                    Begin
                    While 1=1
                    Begin
                    IF PATINDEX('% %',@input) = 0 BREAK
                    SET @input = replace(@input, ' ',' ')
                    End
                    SET @input = ltrim(rtrim(@in put))
                    Return @input
                    End
                    -------------------------------
                    -- select dbo.replace_bla nk('hello is this the one you looking at. ')

                    Comment

                    Working...