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: Yesterday @ 9:57 PM
Points: 32,906, Visits: 26,790
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."

For better, quicker answers on T-SQL questions, click on the following...
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 #819042
Posted Sunday, November 15, 2009 3:07 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 9:50 AM
Points: 2,019, Visits: 4,923
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: Yesterday @ 9:57 PM
Points: 32,906, Visits: 26,790
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."

For better, quicker answers on T-SQL questions, click on the following...
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 #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: Thursday, February 21, 2013 6:30 AM
Points: 562, Visits: 1,404
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: Wednesday, April 24, 2013 5:02 AM
Points: 2,365, Visits: 1,825
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: Wednesday, May 08, 2013 3:35 AM
Points: 224, Visits: 629
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
____________________________________________________


Excel Database Tasks V2 ..
Multi user data edit Task based solution - Excel with SQL Server / Access / MySQL ..


Version 2.5 released - See Blog
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: Friday, February 15, 2013 7:29 AM
Points: 509, Visits: 718
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, May 13, 2013 2:19 PM
Points: 242, Visits: 656
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: Wednesday, May 08, 2013 3:35 AM
Points: 224, Visits: 629
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
____________________________________________________


Excel Database Tasks V2 ..
Multi user data edit Task based solution - Excel with SQL Server / Access / MySQL ..


Version 2.5 released - See Blog
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: Wednesday, May 08, 2013 3:35 AM
Points: 224, Visits: 629
Actually typo in posted code, the second REPLACE is also restricted to 6 spaces, I wonder why?

Cheers


C# Gnu
____________________________________________________


Excel Database Tasks V2 ..
Multi user data edit Task based solution - Excel with SQL Server / Access / MySQL ..


Version 2.5 released - See Blog
Post #819278
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse