Solved

SQL: Pull name from substring .

Posted on 2010-08-24
13
260 Views
Last Modified: 2012-05-10
I am looking to pull out name from a substring, have tried a couple idea's but getting held up on the first name:

Displayed as: Doe, Joseph Moe (Joseph)
Result: Joseph Doe

Any help that can be provided would be much appreciated!
0
Comment
Question by:Levi Martin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
  • +2
13 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33514393
The moment you hit a first name with >1 words, you are sunk.  Also, can you guarantee that each value always follows:[][]   [] denotes optional
0
 

Author Comment

by:Levi Martin
ID: 33514420
That's my battle, is I'm trying to capture what's in the parenthesis as the first name. Yes, it will always follow that format:

LastName, FirstName MiddleName (FirstName)
0
 
LVL 10

Assisted Solution

by:Jini Jose
Jini Jose earned 100 total points
ID: 33514486
kindly try this below code

declare @a varchar(250)
set @a='Doe, Joseph Moe (Joseph)'

select substring(@a,CHARINDEX(',', @a)+1,CHARINDEX(' ', @a,CHARINDEX(',', @a)+2)-CHARINDEX(',', @a)) + ' ' +
substring(@a,0,CHARINDEX(',', @a))
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 400 total points
ID: 33514506
SELECT OriginalName, LEFT(OriginalName, CHARINDEX(',', OriginalName) - 1) + ' ' +       SUBSTRING(OriginalName, CHARINDEX('(', OriginalName) + 1, CHARINDEX(')', OriginalName) - CHARINDEX('(', OriginalName) - 1) AS RedoneFROM SomeTable
0
 
LVL 6

Expert Comment

by:LCSandman8301
ID: 33514521
not to knock it but the way names are going we may have ( in the names soon but check out the code below
select substring(fullname, charindex('(', fullname), charindex(')', fullname) - charindex('(', fullname))
from table1

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33514546
gmailjini,I think your syntax is adding some extraneous spaces.  Your example has a leading space, and two spaces between the first name and surname.:)Patrick
0
 
LVL 16

Expert Comment

by:vdr1620
ID: 33514571
SELECT SUBSTRING(ColumnName,CHARINDEX('(',ColumnName)+1,CHARINDEX(')',ColumnName)-(CHARINDEX('(',ColumnName+1))
+ ' '+
SUBSTRING(ColumnName', 1, CHARINDEX(',',ColumnName)-1)
0
 

Author Closing Comment

by:Levi Martin
ID: 33514606
First post was great, had an extra space which original poster caught. That's so much for all the follow-up experts!!
0
 
LVL 10

Expert Comment

by:Jini Jose
ID: 33514607
:)
0
 
LVL 10

Expert Comment

by:Jini Jose
ID: 33514658
hi matthewspatrick

what happened if the name does not have any brackets ?
0
 

Author Comment

by:Levi Martin
ID: 33514681
The main IT team here using this name as a P_KEY instead of employee # (don't get me going on that) but they at least always have the first name parenthesis. Seriously, can't thank those poster to this board enough, thanks everyone for the quick follow-up!
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 33515309
>>what happened if the name does not have any brackets ?What happens is, the query fails, but that's why I asked the Asker to guarantee that it would always be there :)Volcrom19: you're most welcome!
0
 
LVL 10

Expert Comment

by:Jini Jose
ID: 33515325
:)
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
find SQL job run average duration 24 54
Many to one in one row 2 38
Upgrading to SQL Server 2015 Express 2 30
How come this XML node is not read? 3 27
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question