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)
 using a function in a loop?

Author  Topic 

brendita
Starting Member

38 Posts

Posted - 2005-10-20 : 17:55:29
Hello. I need a little bit of help here. I am trying to use the following function: http://msdn.microsoft.com/library/en-us/dnsqlmag01/html/treatyourselfListing_01.txt to split names that I have in a table.

I then have this:

declare @CustId varchar(100)
declare @RowNum int
declare CustList cursor for
select top 10 defendant_1 from CaseInformation
OPEN CustList
FETCH NEXT FROM CustList
INTO @CustId
set @RowNum = 0
WHILE @@FETCH_STATUS = 0
BEGIN
set @RowNum = @RowNum + 1
--set @CustId = (SELECT idx, value FROM dbo.fn_Split(@CustId, 'space')

print @CustId
FETCH NEXT FROM CustList
INTO @CustId
END
CLOSE CustList
DEALLOCATE CustList


I just can't figure out how to extract the names. Any ideas on how I would do that?

Thanks for your time!

brendita
Starting Member

38 Posts

Posted - 2005-10-20 : 18:25:39
I'm going to use this, unless anyone has better ideas:


UPDATE caseinformation
SET Firstname = SUBSTRING (defendant_1, 1, (CHARINDEX(' ', defendant_1) - 0))

UPDATE caseinformation
SET Lastname = LTRIM(right(Defendant_1,charindex(' ',reverse(Defendant_1))))


I lose all middle initials on this, but I don't know how else to do it. So I'm not going to worry about it, but if you have extra time, feel free to offer some help :)
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-10-20 : 19:58:07
Just an FYI:

http://www.sqlteam.com/item.asp?ItemID=2652

No loops, no cursors, no UDF's.
Go to Top of Page

nathans
Aged Yak Warrior

938 Posts

Posted - 2005-10-20 : 20:01:18
Just for fun... try playing with parsename:
** This is just an example. I didnt consider names like Michael J von Buren III


Props to Rob and Merkin Graz (sorry Graz ): [url]http://www.sqlteam.com/item.asp?ItemID=15044[/url]




declare @table table (table_id int identity(1,1), defendant varchar(100))
insert into @table (defendant)
select 'Nathan L Skerl' union
select 'Adam J Skerl' union
select 'Natalie R Skerl' union
select 'John Skerl'


-- initial state
select * from @table

-- replace space with period
update @table
set defendant = case
when LEN(LTRIM(RTRIM(defendant))) - LEN(REPLACE(LTRIM(RTRIM(defendant)), ' ', '')) = 2
then replace(defendant, ' ', '.')
when LEN(LTRIM(RTRIM(defendant))) - LEN(REPLACE(LTRIM(RTRIM(defendant)), ' ', '')) = 1
then replace(defendant, ' ', '..')
else defendant
end


select parsename(defendant,3) as first_name,
case
when len(parsename(defendant,2)) = 1 then parsename(defendant,2)
else null
end as middle_initial,
parsename(defendant,1) as last_name
from @table


Nathan Skerl
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2005-10-20 : 20:12:58
quote:
Props to Rob and Graz: http://www.sqlteam.com/item.asp?ItemID=15044 height="1" noshade id="quote">
Ummmmm, Damian (Merkin) wrote that article, maybe he should get the props?
Go to Top of Page

blindman
Master Smack Fu Yak Hacker

2365 Posts

Posted - 2005-10-21 : 00:26:37
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=56499
Should parse any Western style formal name. 'Nathan L Skerl', 'Skerl, Nathan L.', 'Dr. Nathan Lazarus Skerl III'. Whatever. You can format the name in a specific style, or just return one portion.
Go to Top of Page

brendita
Starting Member

38 Posts

Posted - 2005-10-21 : 18:13:23
Dang, that article (http://www.sqlteam.com/item.asp?ItemID=15044) is perfect. Thanks guys!
Go to Top of Page

Beaumondo
Starting Member

2 Posts

Posted - 2005-10-22 : 11:07:30
This is really to confirm what others have put.

The folloiwng example is using The Northwinds.Customer table

SUBSTRING(ContactName, 1,(CHARINDEX( ' ', ContactName))) AS [First Name]
FROM Customers

There is more info in the online help about the CHARINDEX and SUBSTRING functions.
Go to Top of Page
   

- Advertisement -