Solved

SQL Help

Posted on 2011-03-14
7
291 Views
Last Modified: 2012-08-14
I'm creating user names based on the following:

substring(firstname,1,1)+isnull(substring(middlename,1,1),'')+lastname

So the user names are usually BAMoses or KMGivler or DARexroth which works well.

What I'm running into is last names that are hyphenated like Brabham-Lawrence which then creates the user name IABrabham-Lawrence or the last name will have an apostrophe in it like O'Donnell which then returns the user name of PDO'Donnell.

I need assistance accounting for this by either truncating the name before the hyphen (which I prefer) and removing the apostrophe.

Thanks for any guidance!
0
Comment
Question by:jasonbrandt3
  • 4
  • 2
7 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 35128748

try


substring(firstname,1,1)+isnull(substring(middlename,1,1),'')
REPLACE((case when (CHARINDEX('-', LastName, 1)) > 0 then 
           LEFT(LastName, CHARINDEX('-', LastName, 1)-1) 
           ELSE LastNameEND), '''', '')

Open in new window

0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35128758
i missed the +
substring(firstname,1,1)+isnull(substring(middlename,1,1),'') +
REPLACE((case when (CHARINDEX('-', LastName, 1)) > 0 then 
           LEFT(LastName, CHARINDEX('-', LastName, 1)-1) 
           ELSE LastNameEND), '''', '')

Open in new window

0
 
LVL 3

Expert Comment

by:CarlsbergFTW
ID: 35128761
have you tried using "case" within the section that is generating the last name ?

CASE input_expression
     WHEN when_expression THEN result_expression [ ...n ]
     [ ELSE else_result_expression ]
END
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:jasonbrandt3
ID: 35128955
@ewangoya:

That would work for the hyphen, how would I remove an apostrophe if their last name has one like O'Donnell?
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35128987

That removes the Hyphen as well
REPLACE checks for hyphen and replaces with blank character
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 35128997


substring(firstname,1,1)+isnull(substring(middlename,1,1),'') +
REPLACE((case when (CHARINDEX('-', LastName, 1)) > 0 then
           LEFT(LastName, CHARINDEX('-', LastName, 1)-1)
           ELSE LastName END), '''', '')
0
 

Author Closing Comment

by:jasonbrandt3
ID: 35129023
Perfect!  I just ran it, exactly how I wanted it.  Thanks so much for the help!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now