Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Strange result with REPLACE

Author  Topic 

savvy95
Starting Member

23 Posts

Posted - 2005-05-02 : 12:22:23
I have a table named Members_ with a field named FullName. Somebody imported into the FullName_ field records with varying spaces between the first name and last name.

FullName_
----------------------------------
Pete[sp][sp][sp][sp][sp][sp]Smith
Sarah[sp][sp][sp][sp]Parker
Philip[sp][sp]Gonzalez


The names are figments of my imagination.
I had to put [sp] because the code on this website ignores spaces. I'm glad I previewed first.

When I run in TSQL:
select replace(fullName_, ' ', '') from members_
where fullName_ like '% %'

I get:
Pete Smith
SarahParker
PhilipGonzalez

Some names are as I'd like them to be; with a space between the first and last name. Others have no space.

If anyone can shed light on what I'm doing wrong, I would be quite appreciative.

mpetanovitch
Yak Posting Veteran

52 Posts

Posted - 2005-05-02 : 13:34:48
I simulated your above situation and my result set had the names with no spaces. So I would look at making sure that they are actually spaces(CHAR(32)) in between the first and last names.

Mike Petanovitch
Go to Top of Page

savvy95
Starting Member

23 Posts

Posted - 2005-05-02 : 13:46:36
Thanks Mike. How can I ensure that spaces are being used? I don't understand your clarification of "(CHAR(32))"? Please explain.
Go to Top of Page

savvy95
Starting Member

23 Posts

Posted - 2005-05-02 : 14:24:32
After asking I looked. I guess I should have done it the other way around. Look then ask.
I found how to identify the ASCII character, but I'm still in trouble; because there are spaces between the first and last name.

I ran this to see:
SET TEXTSIZE 0
-- Create variables for the character string and for the current
-- position in the string.
DECLARE @position int, @string char(80)
-- Initialize the current position and the string variables.
SET @position = 1
SET @string = 'Sarah Parker
WHILE @position <= DATALENGTH(@string)
BEGIN
SELECT ASCII(SUBSTRING(@string, @position, 1)),
CHAR(ASCII(SUBSTRING(@string, @position, 1)))
SET @position = @position + 1
END
GO

And the result set included 32 for the spaces as you identified.

What else can I do? Any suggestions?
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-05-02 : 15:36:24
You might have tabs or some other character separating the names.
If they are truly separated by CHAR(32) then your solution should work.

You might try doing something like this to remove everything except A-Z or a-z... But do you really want NO spaces, or 1 space consistently? Keep in mind, this solution would remove thing slike ['] or [-]... O'Leary or Smith-Barney would be jacked, but you could easily add those to the IF statement below.


DECLARE @char VARCHAR(1),
@text_in VARCHAR(200),
@text_out VARCHAR(200),
@pos INT

SELECT @text_in = 'Nathan Skerl' -- separated by (2) spaces and (2) tabs

If @text_in IS NOT NULL
BEGIN
SELECT @char = '',
@text_out = '',
@pos = 1

WHILE @pos <= DATALENGTH(@text_in)
BEGIN
-- set @char to first character
SET @char = SUBSTRING(@text_in, @pos, 1)

-- if @char <> A-Z or a-z replace with ''
IF ASCII(@char) Between 65 And 90 -- uppercase A-Z
OR ASCII(@char) Between 97 And 122 -- lowercase a-z
BEGIN
SET @char = @char
END ELSE
BEGIN
SET @char = ''
END

-- build output string, increment position
SET @text_out = (@text_out + @char)
SET @pos = (@pos + 1)
END


END
SELECT @text_out
Go to Top of Page

savvy95
Starting Member

23 Posts

Posted - 2005-05-02 : 17:12:51
Thanks Nathan. Indeed I want only 1 space between the first and last name. Your solution worked; but, how do I get the field name FullName_ into your solution so that instead of 'Nathan Skerl' I can use FullName_. I noticed from further research that many last names are the 20th character from the left (while the first name varies) and others only have 2 spaces between the first and last names:

Some Look like this:
Pete[sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp]Smith
(Smith starts on the 20th character)
Sarah[sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp]Parker
(Parker starts on the 20th character)
Philip[sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp]Gonzalez
(Gonzalez starts on the 20th character)
Ed[sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp][sp]Smith'
(Smith also starts on the 20th character)
Charlene-Gertrude[sp][sp][sp]Rye'
(Rye starts on the 20th character)

Others look like this:
Pete[sp][sp]Smith
Sarah[sp][sp]Parker
Philip[sp][sp]Gonzalez


All your help is appreciated
PS. In the QuickReply message box HTML is OFF. How do I turn it on, so I can represent spaces more accurately?
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-05-02 : 17:16:30
dont do quick reply. Click 'Reply to Topic' and use the code brackets.
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-05-02 : 17:53:09
try...




declare @string varchar(100),
@pos int

select @string = 'Nathan Skerl',
@pos = charindex(char(32), @string)

-- replace any TAB with ''
select @string = replace(@string, char(9), '')

-- remove all but 1st space
select @string = substring(@string, 1, @pos - 1) + char(32) + ltrim(substring(@string, @pos, len(@string)))

select @string
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-05-02 : 17:54:40
and also think about putting first & last name in separate columns...
Go to Top of Page

savvy95
Starting Member

23 Posts

Posted - 2005-05-02 : 18:34:15
[code]Thanks for the quick reply. Spaces work now
Thanks again Nathan; but how do I get FullName_ into your code.

I've been going about this the wrong way! Thank you. When you asked about splitting
firstname/lastname, I realized I can delete the FullName and build it again based
on firstname/lastname fields, which I already have.

Sometimes I have to talk to people to figure things out. thanks again.

Savvy95
[/code]
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-05-02 : 20:33:30
Im assuming you do not have a unique id on members_, you can use a table variable to stage the names, add an id column, and iterate through the results to perform your string manipulation.


-- your table
declare @members_ table (fullname_ varchar(50))
insert into @members_
select 'nathan skerl' union
select 'deez nutz' union
select 'michael jackson'

-- temp staging table for string transformations
declare @temp table (test_id int identity(1,1), full_name varchar(50))
declare @id int,
@pos int

-- insert all names into temp without any TABS in name
insert into @temp select replace(fullname_, char(9), '') from @members_

-- get max(test_id) to begin iteration
select @id = max(test_id) from @temp

while @id > 0
begin
-- get position of space
select @pos = charindex(char(32), full_name)
from @temp
where test_id = @id

-- do update, retain only 1 space
update @temp
set full_name = substring(full_name, 1, @pos - 1) + char(32) + ltrim(substring(full_name, @pos, len(full_name)))
from @temp
where test_id = @id

-- decrement id and continue loop
set @id = @id - 1
end

-- return results
select * from @temp
Go to Top of Page
   

- Advertisement -