Solved

Extracting Name from Email

Posted on 2014-10-31
3
111 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
ID: 40415337
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
ID: 40415341
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:
Scott Pletcher earned 500 total points
ID: 40415883
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Update one table with results from another table in SQL 6 40
What is this datetime? 1 29
SQL Rewrite without the NULLIF 4 25
T-SQL Query - Group By Year 3 31
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 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