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. Remove Extra Whitespace between Words -- But Leave One Space in a Certain Location

Remove Extra Whitespace between Words -- But Leave One Space in a Certain Location

Scheduled Pinned Locked Moved Database
questiondatabasesql-serversysadmin
8 Posts 6 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.
  • Brian C HartB Offline
    Brian C HartB Offline
    Brian C Hart
    wrote on last edited by
    #1

    Dear All, Forgive me for asking what must seem to most like an obvious question, but I am a SQL server newbie. I am using SQL Server 2005 and I have a certain field which contains both a city and a state in one file; i.e., Portland, Oregon will be listed as

    CityAndState

    PORTLAND OR

    Notice that this is a single VARCHAR column and there are a whole bunch of extra spaces in between the 'PORTLAND' and 'OR'. How do I get it so I have

    CityAndState

    PORTLAND OR

    Sincerely Yours,
    Brian Hart

    L S T H 4 Replies Last reply
    0
    • Brian C HartB Brian C Hart

      Dear All, Forgive me for asking what must seem to most like an obvious question, but I am a SQL server newbie. I am using SQL Server 2005 and I have a certain field which contains both a city and a state in one file; i.e., Portland, Oregon will be listed as

      CityAndState

      PORTLAND OR

      Notice that this is a single VARCHAR column and there are a whole bunch of extra spaces in between the 'PORTLAND' and 'OR'. How do I get it so I have

      CityAndState

      PORTLAND OR

      Sincerely Yours,
      Brian Hart

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      you could do a search for double space, then replace double space by single space, and repeat until nothing gets found. a possible improvement would look for 8 spaces and replace them by 1; then 4 by 1; then 2 by 1. However, it is simply wrong to store two pieces of data in a single field, you really should split them permanently and hence avoid all future formatting issues. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      S 1 Reply Last reply
      0
      • Brian C HartB Brian C Hart

        Dear All, Forgive me for asking what must seem to most like an obvious question, but I am a SQL server newbie. I am using SQL Server 2005 and I have a certain field which contains both a city and a state in one file; i.e., Portland, Oregon will be listed as

        CityAndState

        PORTLAND OR

        Notice that this is a single VARCHAR column and there are a whole bunch of extra spaces in between the 'PORTLAND' and 'OR'. How do I get it so I have

        CityAndState

        PORTLAND OR

        Sincerely Yours,
        Brian Hart

        S Offline
        S Offline
        Scubapro
        wrote on last edited by
        #3

        Use the REPLACE function. Check here.

        N 1 Reply Last reply
        0
        • L Luc Pattyn

          you could do a search for double space, then replace double space by single space, and repeat until nothing gets found. a possible improvement would look for 8 spaces and replace them by 1; then 4 by 1; then 2 by 1. However, it is simply wrong to store two pieces of data in a single field, you really should split them permanently and hence avoid all future formatting issues. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          S Offline
          S Offline
          Scubapro
          wrote on last edited by
          #4

          I could say 'That is horrible', but I won't. Ooops, just did it.

          1 Reply Last reply
          0
          • Brian C HartB Brian C Hart

            Dear All, Forgive me for asking what must seem to most like an obvious question, but I am a SQL server newbie. I am using SQL Server 2005 and I have a certain field which contains both a city and a state in one file; i.e., Portland, Oregon will be listed as

            CityAndState

            PORTLAND OR

            Notice that this is a single VARCHAR column and there are a whole bunch of extra spaces in between the 'PORTLAND' and 'OR'. How do I get it so I have

            CityAndState

            PORTLAND OR

            Sincerely Yours,
            Brian Hart

            T Offline
            T Offline
            T M Gray
            wrote on last edited by
            #5

            Turn it into two columns and then you can format it however you want DECLARE @city varchar(20) Set @city= 'Portland OR' select SUBSTRING(@city, 1,CHARINDEX(' ', @city)) as [city], REVERSE((SUBSTRING(REVERSE(@city),1,CHARINDEX(' ', REVERSE(@city))))) as [state] This assumes no trailing spaces after OR. If there are add some TRIM() in there.

            1 Reply Last reply
            0
            • Brian C HartB Brian C Hart

              Dear All, Forgive me for asking what must seem to most like an obvious question, but I am a SQL server newbie. I am using SQL Server 2005 and I have a certain field which contains both a city and a state in one file; i.e., Portland, Oregon will be listed as

              CityAndState

              PORTLAND OR

              Notice that this is a single VARCHAR column and there are a whole bunch of extra spaces in between the 'PORTLAND' and 'OR'. How do I get it so I have

              CityAndState

              PORTLAND OR

              Sincerely Yours,
              Brian Hart

              H Offline
              H Offline
              Hiren solanki
              wrote on last edited by
              #6

              SELECT REPLACE(CityAndState,' ','');

              Code just replaces DualSpace with NoSpace.

              Regards, Hiren. "The more we give of anything, the more we shall get back." - Grace Speare (you can consider this quote while giving vote also) Microsoft Dynamics CRM

              N 1 Reply Last reply
              0
              • H Hiren solanki

                SELECT REPLACE(CityAndState,' ','');

                Code just replaces DualSpace with NoSpace.

                Regards, Hiren. "The more we give of anything, the more we shall get back." - Grace Speare (you can consider this quote while giving vote also) Microsoft Dynamics CRM

                N Offline
                N Offline
                NeverHeardOfMe
                wrote on last edited by
                #7

                This is no good if there are an even number of spaces between words.

                1 Reply Last reply
                0
                • S Scubapro

                  Use the REPLACE function. Check here.

                  N Offline
                  N Offline
                  NeverHeardOfMe
                  wrote on last edited by
                  #8

                  I would be intrigued to know why this answer (above) has been down-voted so much. I think it links to a good solution.

                  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