Solved

Extracting Name from Email

Posted on 2014-10-31
3
114 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
[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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
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…

728 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