Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

REPLACE Multiple Spaces with One Expand / Collapse
Author
Message
Posted Saturday, November 14, 2009 12:21 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,994, Visits: 31,513
Comments posted to this topic are about the item REPLACE Multiple Spaces with One

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #819042
Posted Sunday, November 15, 2009 3:07 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 26, 2014 12:43 PM
Points: 2,109, Visits: 5,419
Very nice solution and a great explanation for the solution.

Adi


--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #819110
Posted Sunday, November 15, 2009 10:26 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,994, Visits: 31,513
Thanks Adi... it's an old solution and it's not mine. I've just never seen anyone explain it before and thought an explanation was long overdue especially since it's been coming up a lot on the forums, again. A lot of the "answer" posts have included a substantial amount of RBAR and, of course, that drove me to write the article.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #819136
Posted Monday, November 16, 2009 2:03 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, August 15, 2014 1:18 AM
Points: 566, Visits: 1,598
Another (very good) example of something that initially appears to be a horrendous problem having an elegant and relatively simple solution.

Must add to briefcase


-------------------------------------------------------------------------
Normal chaos will be resumed as soon as possible.
Post #819240
Posted Monday, November 16, 2009 2:19 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 4:53 AM
Points: 2,366, Visits: 1,844
Nice one jeff.

"Keep Trying"
Post #819251
Posted Monday, November 16, 2009 2:43 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, August 15, 2014 12:07 PM
Points: 225, Visits: 645
Hi
Does this simple replace not work then?

REPLACE(@SomeText, '  ',' ')

which is actually :

REPLACE(@SomeText, '[space][space]','[space]')

i.e. replace two spaces with one..

This seams to work fine for me, but I could well be missing something?


C# Gnu
____________________________________________________


Multi user data edit / data transfer Excel Task based solution
Version 2.7 released June 2013
Post #819263
Posted Monday, November 16, 2009 2:50 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, July 22, 2013 11:59 AM
Points: 509, Visits: 719
Jeff

Nice article - I just hope that when I *finally* get round to writing mine then it's anywhere near one tenth as good as yours generally are...

M


Atlantis Interactive - SQL Server Tools
My blog
Why I wrote a sql query analyzer clone
Post #819265
Posted Monday, November 16, 2009 3:13 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, June 16, 2014 4:18 PM
Points: 242, Visits: 678
Richard Briggs (11/16/2009)
Hi
Does this simple replace not work then?

REPLACE(@SomeText, '  ',' ')

which is actually :

REPLACE(@SomeText, '[space][space]','[space]')

i.e. replace two spaces with one..

This seams to work fine for me, but I could well be missing something?


sure, it works. if you only have 2 spaces it will work. the problem on the article however is for when u have a random amount of spaces.
with your example you would have to manually add a REPLACE for each extra space


--
Thiago Dantas
@DantHimself
Post #819272
Posted Monday, November 16, 2009 3:18 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, August 15, 2014 12:07 PM
Points: 225, Visits: 645
I think you need to try this out ... the above comment (sorry dant12) is not correct:

It looks like the following will replace up to six spaces
REPLACE(@SomeText, '  ',' ')

And the following will sort the rest out :
REPLACE(REPLACE(@SomeText, '  ',' '),'  ',' ')

try this one :
DECLARE @sample VARCHAR(1000)
SET @sample= 'ALKSDKLKJ LSJD ASD LSD S D DJD D D D D D D'

SELECT REPLACE(REPLACE(@sample,' ',' '),' ',' ')



C# Gnu
____________________________________________________


Multi user data edit / data transfer Excel Task based solution
Version 2.7 released June 2013
Post #819275
Posted Monday, November 16, 2009 3:27 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, August 15, 2014 12:07 PM
Points: 225, Visits: 645
Actually typo in posted code, the second REPLACE is also restricted to 6 spaces, I wonder why?

Cheers


C# Gnu
____________________________________________________


Multi user data edit / data transfer Excel Task based solution
Version 2.7 released June 2013
Post #819278
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse