Unix Technical Forum

Replace Multiple Spaces with One Space?

This is a discussion on Replace Multiple Spaces with One Space? within the SQL Server forums, part of the Microsoft SQL Server category; --> I need to map several columns of data from one database to another where the data contains multiple spaces ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 01:19 PM
Joe Cool
 
Posts: n/a
Default Replace Multiple Spaces with One Space?

I need to map several columns of data from one database to another
where the data contains multiple spaces (once occurance of a variable
number or spaces) that I need to replace with a single space. What
would be the most efficient way to do this? I am using SQL2K. I was
thinking a function since I know of no single Transact-SQL command
that can accomplish this task.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 01:19 PM
Russ Rose
 
Posts: n/a
Default Re: Replace Multiple Spaces with One Space?


"Joe Cool" <joecool@home.net> wrote in message
news:5pb7t2dkt37mgh0cqnm3f9luuu1rvhglkm@4ax.com...
>I need to map several columns of data from one database to another
> where the data contains multiple spaces (once occurance of a variable
> number or spaces) that I need to replace with a single space. What
> would be the most efficient way to do this? I am using SQL2K. I was
> thinking a function since I know of no single Transact-SQL command
> that can accomplish this task.


DECLARE @FieldName varchar(5000)
SET @FieldName = ' Reduce any number of' + SPACE(512) +
'spaces up to 512 in
a row to a single space
'

--You can reduce layers of REPLACE depending how many extra spaces you
expect.

SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRI M(@FieldName)),
SPACE(16), ' '), SPACE(8), ' '), SPACE(4), ' '), SPACE(2), ' '), SPACE(2), '
')


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 02:29 PM
Joe Cool
 
Posts: n/a
Default Re: Replace Multiple Spaces with One Space?

On Wed, 14 Feb 2007 20:47:37 -0600, "Russ Rose" <russrose@hotmail.com>
wrote:

>
>"Joe Cool" <joecool@home.net> wrote in message
>news:5pb7t2dkt37mgh0cqnm3f9luuu1rvhglkm@4ax.com.. .
>>I need to map several columns of data from one database to another
>> where the data contains multiple spaces (once occurance of a variable
>> number or spaces) that I need to replace with a single space. What
>> would be the most efficient way to do this? I am using SQL2K. I was
>> thinking a function since I know of no single Transact-SQL command
>> that can accomplish this task.

>
>DECLARE @FieldName varchar(5000)
>SET @FieldName = ' Reduce any number of' + SPACE(512) +
>'spaces up to 512 in
>a row to a single space
>'
>
>--You can reduce layers of REPLACE depending how many extra spaces you
>expect.
>
>SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRI M(@FieldName)),
>SPACE(16), ' '), SPACE(8), ' '), SPACE(4), ' '), SPACE(2), ' '), SPACE(2), '
>')
>


I believe you misunderstood my question. Here are some sample values:

"SMITH JR" (5 spaces)
"JONES JR" (7 spaces)

Desired result"

"SMITH JR"
"JONES JR"

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 02:29 PM
Ed Murphy
 
Posts: n/a
Default Re: Replace Multiple Spaces with One Space?

Joe Cool wrote:

> On Wed, 14 Feb 2007 20:47:37 -0600, "Russ Rose" <russrose@hotmail.com>
> wrote:
>
>> "Joe Cool" <joecool@home.net> wrote in message
>> news:5pb7t2dkt37mgh0cqnm3f9luuu1rvhglkm@4ax.com...
>>> I need to map several columns of data from one database to another
>>> where the data contains multiple spaces (once occurance of a variable
>>> number or spaces) that I need to replace with a single space. What
>>> would be the most efficient way to do this? I am using SQL2K. I was
>>> thinking a function since I know of no single Transact-SQL command
>>> that can accomplish this task.

>> DECLARE @FieldName varchar(5000)
>> SET @FieldName = ' Reduce any number of' + SPACE(512) +
>> 'spaces up to 512 in
>> a row to a single space
>> '
>>
>> --You can reduce layers of REPLACE depending how many extra spaces you
>> expect.
>>
>> SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRI M(@FieldName)),
>> SPACE(16), ' '), SPACE(8), ' '), SPACE(4), ' '), SPACE(2), ' '), SPACE(2), '
>> ')
>>

>
> I believe you misunderstood my question. Here are some sample values:
>
> "SMITH JR" (5 spaces)
> "JONES JR" (7 spaces)
>
> Desired result"
>
> "SMITH JR"
> "JONES JR"


Looks to me like his answer produces the desired result. What problem
do you see with it?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 02:29 PM
Russ Rose
 
Posts: n/a
Default Re: Replace Multiple Spaces with One Space?


"Ed Murphy" <emurphy42@socal.rr.com> wrote in message
news:45d51798$0$28101$4c368faf@roadrunner.com...
> Joe Cool wrote:
>
>> On Wed, 14 Feb 2007 20:47:37 -0600, "Russ Rose" <russrose@hotmail.com>
>> wrote:
>>
>>> "Joe Cool" <joecool@home.net> wrote in message
>>> news:5pb7t2dkt37mgh0cqnm3f9luuu1rvhglkm@4ax.com...
>>>> I need to map several columns of data from one database to another
>>>> where the data contains multiple spaces (once occurance of a variable
>>>> number or spaces) that I need to replace with a single space. What
>>>> would be the most efficient way to do this? I am using SQL2K. I was
>>>> thinking a function since I know of no single Transact-SQL command
>>>> that can accomplish this task.
>>> DECLARE @FieldName varchar(5000)
>>> SET @FieldName = ' Reduce any number of' + SPACE(512) +
>>> 'spaces up to 512 in a
>>> row to a single space '
>>>
>>> --You can reduce layers of REPLACE depending how many extra spaces you
>>> expect.
>>>
>>> SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRI M(@FieldName)),
>>> SPACE(16), ' '), SPACE(8), ' '), SPACE(4), ' '), SPACE(2), ' '),
>>> SPACE(2), ' ')

>>
>> I believe you misunderstood my question. Here are some sample values:
>>
>> "SMITH JR" (5 spaces)
>> "JONES JR" (7 spaces)
>>
>> Desired result"
>>
>> "SMITH JR"
>> "JONES JR"

>
> Looks to me like his answer produces the desired result. What problem
> do you see with it?


It was a bit of overkill... maybe this will be closer to what you want.


CREATE FUNCTION dbo.SingleSpace(@str varchar(8000))

RETURNS varchar(8000)

AS
--Usage
--SELECT dbo.SingleSpace('Function replaces any and all spaces up
to 16 in a row with a single space ')

BEGIN
DECLARE @s varchar(8000)
SELECT @s = REPLACE(REPLACE(REPLACE(RTRIM(LTRIM(@str)), SPACE(4), ' '),
SPACE(2), ' '), SPACE(2), ' ')
RETURN @s
END


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-01-2008, 02:30 PM
Ed Murphy
 
Posts: n/a
Default Re: Replace Multiple Spaces with One Space?

Russ Rose wrote:
> "Ed Murphy" <emurphy42@socal.rr.com> wrote in message
> news:45d51798$0$28101$4c368faf@roadrunner.com...
>> Joe Cool wrote:
>>
>>> On Wed, 14 Feb 2007 20:47:37 -0600, "Russ Rose" <russrose@hotmail.com>
>>> wrote:
>>>
>>>> "Joe Cool" <joecool@home.net> wrote in message
>>>> news:5pb7t2dkt37mgh0cqnm3f9luuu1rvhglkm@4ax.com...
>>>>> I need to map several columns of data from one database to another
>>>>> where the data contains multiple spaces (once occurance of a variable
>>>>> number or spaces) that I need to replace with a single space. What
>>>>> would be the most efficient way to do this? I am using SQL2K. I was
>>>>> thinking a function since I know of no single Transact-SQL command
>>>>> that can accomplish this task.
>>>> DECLARE @FieldName varchar(5000)
>>>> SET @FieldName = ' Reduce any number of' + SPACE(512) +
>>>> 'spaces up to 512 in a
>>>> row to a single space '
>>>>
>>>> --You can reduce layers of REPLACE depending how many extra spaces you
>>>> expect.
>>>>
>>>> SELECT REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRI M(@FieldName)),
>>>> SPACE(16), ' '), SPACE(8), ' '), SPACE(4), ' '), SPACE(2), ' '),
>>>> SPACE(2), ' ')
>>> I believe you misunderstood my question. Here are some sample values:
>>>
>>> "SMITH JR" (5 spaces)
>>> "JONES JR" (7 spaces)
>>>
>>> Desired result"
>>>
>>> "SMITH JR"
>>> "JONES JR"

>> Looks to me like his answer produces the desired result. What problem
>> do you see with it?

>
> It was a bit of overkill... maybe this will be closer to what you want.
>
>
> CREATE FUNCTION dbo.SingleSpace(@str varchar(8000))
>
> RETURNS varchar(8000)
>
> AS
> --Usage
> --SELECT dbo.SingleSpace('Function replaces any and all spaces up
> to 16 in a row with a single space ')
>
> BEGIN
> DECLARE @s varchar(8000)
> SELECT @s = REPLACE(REPLACE(REPLACE(RTRIM(LTRIM(@str)), SPACE(4), ' '),
> SPACE(2), ' '), SPACE(2), ' ')
> RETURN @s
> END


Doesn't work on 11, 14, or 15 spaces.

Adding an extra REPLACE(..., SPACE(2), ' ') wrapper will fix it, and
extend it to runs up to 26.

Adding an inner 8->1 on top of that extends to 167; 16->1 on top of that
extends to 2447; 32->1 on top of that extends to 77343, more than enough
unless you're messing with TEXT (for which REPLACE doesn't work at all,
IIRC) or VARCHAR(MAX).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-01-2008, 02:31 PM
--CELKO--
 
Posts: n/a
Default Re: Replace Multiple Spaces with One Space?

Do an UPDATE statement with nested REPLACE() functions to change (n)
spaces to 1 space:

UPDATE Foobar
SET mystring = REPLACE (SPACE(2), SPACE(1) ' ,
...
REPLACE (SPACE(<<fib(n)>>), SPACE(1),
mystring)
.. )));

The optimal pattern for the substitutions is a Fibbonnaci series with
the longest string of spaces in the innermost invocation. You have to
pick the right number based on the length of the column. Working out
the math is fun, so enjoy.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-01-2008, 03:43 PM
Blackburn
 
Posts: n/a
Default Re: Replace Multiple Spaces with One Space?

"Russ Rose" <russrose@hotmail.com> wrote:
>



How did you guys miss the recursion boat?

Create function dbo.udf_CondenseSpaces (@str varchar(8000))

Returns varchar(8000)
AS

BEGIN
Declare @s varchar(8000)

Set @s = replace(@str, ' ', ' ')

if charindex(' ', @s) > 0

set @s = dbo.udf_CondenseSpaces(@s)

return @s
END
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-01-2008, 03:43 PM
MikeJ
 
Posts: n/a
Default Re: Replace Multiple Spaces with One Space?

also instead of calling a function many times
create a function or a proct
Sample incomming string

declare @somestring varchar(100)
set @somestring = 'aaa bbb cccc eee fff f gggg h i aa '
--sample guts of function / proc
while charindex(' ',@somestring)>0 begin
set @Somestring = replace(@somestring,' ',' ')
end
print @somestring
---output aaa bbb cccc eee fff f gggg h i aa




"Blackburn" <blackburn@2centsediting.mypants.com> wrote in message
news:1192727141_459@sp12lax.superfeed.net...
> "Russ Rose" <russrose@hotmail.com> wrote:
>>

>
>
> How did you guys miss the recursion boat?
>
> Create function dbo.udf_CondenseSpaces (@str varchar(8000))
>
> Returns varchar(8000)
> AS
>
> BEGIN
> Declare @s varchar(8000)
>
> Set @s = replace(@str, ' ', ' ')
>
> if charindex(' ', @s) > 0
>
> set @s = dbo.udf_CondenseSpaces(@s)
>
> return @s
> END



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:38 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com