Solved

SQL Help

Posted on 2011-03-14
7
328 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
[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
  • 2
7 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
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:Ephraim Wangoya
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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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:Ephraim Wangoya
ID: 35128987

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

Accepted Solution

by:
Ephraim Wangoya 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

632 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