Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 20th, 2004, 04:11 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default CharIndex to split last, first and Mid name

I have several full name and I'd like to split into last, first and mid name please help

Ex:
Smith, John T
Nguyen, Hung F
Johnson, Paul

I'd like to split as following
LastName FirstName Mid
Smith John T
Nguyen Hung F
Johnson Paul

PLease help Thanks
Reply With Quote
  #2 (permalink)  
Old April 21st, 2004, 03:18 AM
Registered User
 
Join Date: Apr 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

if you're using vbscript try this, let say fullname="Smith, John T"
1. replace (,) with ("")
fullname=replace(fullname,",","")

2. split the string where found space (" ")
nameArray=split(fullname," ")

now you're have have array of lastname, firstname and mid where
LastName = nameArray(1)
FirstName = nameArray(2)
Mid = nameArray(3)

I am not sure an Array element starting 0 or 1, if starting nameArray(1) doesn't work try nameArray(0) then.

goodluck
Reply With Quote
  #3 (permalink)  
Old June 27th, 2006, 10:38 AM
Registered User
 
Join Date: Jun 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, I have the same plan.

In fact I did:

' varSCSRID is Lastname + Firstname
splitArray = Split(varSalesRep, " ")
varSCSRID = Replace(UCase(splitArray(1)), "'", "''") & Replace(UCase(splitArray(0)), "'", "''")

The problem becomes, what if you have "bad" data entry and you get first name with 2 or more spaces until the last name?

This happened to me...

I am trying to code a solution...

Possible to Trim the array values?

Reply With Quote
  #4 (permalink)  
Old June 28th, 2006, 07:13 AM
Friend of Wrox
 
Join Date: May 2006
Location: Helsingborg, , Sweden.
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

-- prepare test data
declare @test table (Name varchar(200))

insert @test
select 'Smith, John T' union all
select 'Nguyen, Hung F' union all
select 'Johnson, Paul'

select * from @test

-- do the work
select LEFT(Name, CHARINDEX(', ', Name) - 1) LastName,
        SUBSTRING(Name, CHARINDEX(', ', Name) + 2, CASE WHEN CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) = 0 THEN LEN(Name) + 1 ELSE CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) END - CHARINDEX(', ', Name) - 2) FirstName,
        RIGHT(Name, LEN(Name) - CASE WHEN CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) = 0 THEN LEN(Name) ELSE CHARINDEX(' ', Name, CHARINDEX(', ', Name) + 2) END) Mid
from @test

Reply With Quote
  #5 (permalink)  
Old June 28th, 2006, 08:27 AM
Authorized User
 
Join Date: Sep 2005
Location: , , .
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Another way using PARSENAME

-- prepare test data
declare @test table (Name varchar(200))

insert @test
select 'Smith, John T' union all
select 'Nguyen, Hung F' union all
select 'Johnson, Paul'

select * from @test

-- do the work
select CASE LEN(NAME) -LEN(Replace(NAME,' ','')) WHEN 2 then REPLACE(PARSENAME(REPLACE(Name,' ','.'),3),',','')
        else REPLACE(PARSENAME(REPLACE(Name,' ','.'),2),',','') end LastNAme,
CASE LEN(NAME) -LEN(Replace(NAME,' ','')) WHEN 2 then PARSENAME(REPLACE(Name,' ','.'),2)
        else PARSENAME(REPLACE(Name,' ','.'),1) end FirstNAme,
CASE LEN(NAME) -LEN(Replace(NAME,' ','')) WHEN 2 then PARSENAME(REPLACE(Name,' ','.'),1)
        else '' end MidNAme
from @test



------------------------------------------ http://sqlservercode.blogspot.com/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mid umeshtheone Beginning VB 6 1 June 11th, 2007 02:49 PM
parsing a string using charindex and substring cole SQL Server 2000 3 March 20th, 2007 02:09 AM
Using MID in SQL server rtr1900 Classic ASP Databases 0 December 13th, 2005 12:21 PM
CHARINDEX instead of Instr... mircea Classic ASP Databases 7 July 19th, 2004 01:47 PM
mid-function not working (i did something wrong) Haroldd Classic ASP Basics 3 February 11th, 2004 04:58 AM



All times are GMT -4. The time now is 05:36 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.