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)
 Splitting a word by uppercase.

Author  Topic 

slacker
Posting Yak Master

115 Posts

Posted - 2003-02-17 : 15:27:52
I have a table that contains a name field. The problem is on some of them the first and lastname have been mushed together... The only thing I have to go on is that the first and lastname both have an uppercase first character. How would I go about splitting those up and inserting a space there in sql? In c# i could do it with regular expressions... but I want to do it with sql.

JohnSmith should be John Smith

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-02-17 : 16:11:07
Here's one set-based way to do this; there's probably more efficient ways (i.e., a User-Defined function):



-- table of names with no spaces
declare @Names table ([Name] varchar(100));

-- we need a table of numbers
declare @Ints table (i int identity, [dummy] int);

-- here's some data
insert into @Names
select 'JeffSmith' union
select 'RobVolk' union
select 'BillyJoel' union
select 'MaryJohnson' union
select 'PetePeters'

-- populate our table of numbers with 1-25
insert into @ints(dummy)
select 0 from
@Names N1
cross join @Names N2

-- this will give us the position of the 2nd capital letter
-- in each name:

select [Name], Min(i) as Position
FROM @names
cross join @ints
WHERE
i > 1 AND
ASCII(SUBSTRING([Name],i,1)) between 65 and 90
group by [Name]

-- So, our final query is:
SELECT Left(Name,Position-1) + ' ' + SUBSTRING(Name,Position, Len(Name)-Position + 1)
FROM
(
select [Name], Min(i) as Position
FROM @names
cross join @ints
WHERE
i > 1 AND
ASCII(SUBSTRING([Name],i,1)) between 65 and 90
group by [Name]
) A


- Jeff

Edited by - jsmith8858 on 02/17/2003 16:12:16
Go to Top of Page

byrmol
Shed Building SQL Farmer

1591 Posts

Posted - 2003-02-17 : 17:13:32
slacker,

SQL Server uses some regular expression operators...

This will split simple names apart..


Select Name, LEFT(Name, LEN(Name) - PATINDEX('%[ABCDEFGHIJKLMNOPQRSTUVWXYZ]%', REVERSE(Name)))
+ ' ' + RIGHT(Name, PATINDEX('%[ABCDEFGHIJKLMNOPQRSTUVWXYZ]%', REVERSE(Name))) AS SplitName
FROM Names


You may have to add a COLLATE clause....

EDIT: Forgot the square brackets...

DavidM

"SQL-3 is an abomination.."

Edited by - byrmol on 02/17/2003 17:30:01
Go to Top of Page

slacker
Posting Yak Master

115 Posts

Posted - 2003-02-17 : 17:52:56
Excellent. Thank you much. This will be very useful to me. I have to do these kinds of things alot and I usually end up writing script to parse it. We get data like this that requires alot of duplication checking and are plagued with human typing errors.

Go to Top of Page
   

- Advertisement -