Solved

Extracting Name from Email

Posted on 2014-10-31
3
100 Views
Last Modified: 2014-11-24
Hi

SQL 2008

I have email addresses in a column as follows - they all follow the same format

matt.smith@example.com
susan.smith@hayes.com

I just need to extract the name from it as follows

Matt Smith
Susan Smith

Any help would be appreciated
0
Comment
Question by:halifaxman
3 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
Comment Utility
Here is the code:

select Upper(substring(substring('matt.smith@example.com', 1,charindex('.','matt.smith@example.com')-1),1,1))+ 
             substring(substring('matt.smith@example.com', 1,charindex('.','matt.smith@example.com')-1),2,200) as name,
             
upper(substring(substring('matt.smith@example.com', charindex('.','matt.smith@example.com')+1,(charindex('@','matt.smith@example.com')-charindex('.','matt.smith@example.com'))-1),1,1))+
      substring(substring('matt.smith@example.com', charindex('.','matt.smith@example.com')+1,(charindex('@','matt.smith@example.com')-charindex('.','matt.smith@example.com'))-1) ,2,200) as lastname

Open in new window


you can test it here http://sqlfiddle.com/#!3/d41d8/40668

You just need to replace 'matt.smith@example.com' with you column_name
0
 
LVL 25

Expert Comment

by:Lee Savidge
Comment Utility
If they're always in the form firstname.surname@blah.com then it is quite straightforward, but it looks dirty

declare @e nvarchar(100)
select @e = 'john.smith@fghjk.com'

declare @fn nvarchar(100), @sn nvarchar(100)

select @fn = LEFT(@e, charindex('.', @e) - 1),
       @sn = replace(SUBSTRING(@e, charindex('.', @e), charindex('@', @e) - LEN(@fn) - 1), '.', '')

select @fn, @sn

Open in new window


replace @e with you column name.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
Rather tricky since you have to consider a '.' with no '@', an '@' with no '.', and neither an '@' nor a '.'.

SELECT
    email_address,
    CASE WHEN dot_location = 0 THEN STUFF(email_address, 1, 1, UPPER(LEFT(email_address, 1))) ELSE
        STUFF(LEFT(email_address, dot_location - 1), 1, 1, UPPER(LEFT(email_address, 1))) END AS first_name,
    CASE WHEN dot_location = 0 THEN '' ELSE
        STUFF(SUBSTRING(email_address, dot_location + 1, at_sign_location - dot_location - 1), 1, 1, UPPER(SUBSTRING(email_address, dot_location + 1, 1))) END AS last_name        
FROM (
    SELECT 'matt.smith@example.com' AS email_address UNION ALL
    SELECT 'susan.smith@hayes.com' UNION ALL
    SELECT 'george.washington@mountvernon.com' UNION ALL
    SELECT 'First_name.last_name (but no at sign)' UNION ALL
    SELECT 'somename@butnodot' UNION ALL
    SELECT 'textonlynoatordot'
) AS test_data
CROSS APPLY (
    SELECT
        CASE WHEN CHARINDEX('@', email_address) = 0 THEN LEN(email_address) + 1 ELSE CHARINDEX('@', email_address) END AS at_sign_location,
        CHARINDEX('.', email_address) AS dot_location
) AS assign_alias_names
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

12 Experts available now in Live!

Get 1:1 Help Now