If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Substring

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-18-07, 11:07
smiely smiely is offline
Registered User
 
Join Date: May 2007
Posts: 3
Substring

I want to split "name" column into 3 columns......"lastname" "firstname" "mi" in SQL. Following is the name format....

Abbott,Gregory A.
Abel,Guy C.
Adams,Daniel Lee
Adams,Doris Jean
Adams,Kevin B.
Adams,Lewis Kevin
Adamson Jr.,Gerald G.
Aderhold,R. Chuck
Alden,Bradley Douglas
Aldrich,Daniel J.

Can anybody help me....
Reply With Quote
  #2 (permalink)  
Old 05-18-07, 11:15
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Split on comma, then split on space...
Reply With Quote
  #3 (permalink)  
Old 05-18-07, 11:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
for a more specific solution, you will want to let us know which database system you're using -- postgresql, sybase, db2, whatever -- so that we can move this thread to the appropriate forum
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 05-18-07, 11:38
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Code:
SELECT
  SUBSTR(name,1,POSITION(',' IN name)-1) as lastname
 ,SUBSTR(
    SUBSTR(name,
      POSITION(',' IN name)+1,
      LENGTH(name)),
    1,POSITION(' ' IN
        SUBSTR(name,
        POSITION(',' IN name)+1,
        LENGTH(name))
      )-1
  ) AS firstname
 ,SUBSTR(
    SUBSTR(name,
      POSITION(',' IN name)+1,
      LENGTH(name)),
    POSITION(' ' IN
      SUBSTR(name,
      POSITION(',' IN name)+1,
      LENGTH(name))
    ),LENGTH(name)-
      POSITION(' ' IN name)+1
  ) AS middlename
Reply With Quote
  #5 (permalink)  
Old 05-18-07, 18:06
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
This is what you want to use:
http://sqlblindman.googlepages.com/formatname
To get the Last Name, for instance, you would call: dbo.FormatName([YourNameString], 'L').
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #6 (permalink)  
Old 05-18-07, 19:42
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
assuming, mister blindman sir, that the original poster is running some version of microsoft sql server

that's a fair guess, given that so many people cannot tell the difference between microsft sql server as a dbms product and sql as a language

but this is the sql forum, and not the microsoft sql server forum, so you gots to take that into consideration

charindex, indeed
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 05-19-07, 00:12
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
I have a version for Access as well, and a buddy of mine converted the logic to .NET.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
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 On
Pingbacks are On
Refbacks are On