views:

199

answers:

4

One table has "John Doe <[email protected]>" while another has "[email protected]". Is there a UDF or alternative method that'll match the email address from the first field against the second field?

This isn't going to be production code, I just need it for running an ad-hoc analysis. It's a shame the DB doesn't store both friendly and non-friendly email addresses.

Update: Fixed the formatting, should be < and > on the first one.

+1  A: 

I would split the email addresses on the last space- this should give you the email address. The exact code would depend on your database, but some rough pseudocode:

email = row.email
parts = email.split(" ")
real_email = parts[ len(parts) - 1 ]
Zoomzoom83
A: 

Perhaps the following TSQL code can help you:

DECLARE @email varchar(200)
SELECT @email = 'John Doe [email protected]'

SELECT REVERSE(SUBSTRING(REVERSE(@email), 0,CHARINDEX(' ', REVERSE(@email))))

That statement returns:

[email protected]

Talking through the logic:

  1. Reverse the email column
  2. Find the index of the first ' ' character ... everything up to this point is your actual email address
  3. Substring the column, from the start of the (reversed) string, to the index found at step 2.
  4. Reverse the string again, putting it in the right order.

There might be more elegant ways of doing it, but that would work, and you could therefore use it for one side of your JOIN. It works because email addresses cannot contain spaces, so therefore the last space (or first when you reverse it) will be the separator between your actual email address, and friendly one. As far as I know TSQL does not contain a LastIndexOf() function, which would have been handy to avoid the double Reverse() function calls.

Scott Ferguson
A: 

You could do a join using LOCATE method, something like...

 SELECT * FROM table1 JOIN table2 ON (LOCATE(table2.real_email, table1.friend_email) > 0)
Anthony
As far as I know, there is no LOCATE function in TSQL?
Scott Ferguson
SELECT * FROM table1 JOIN table2 ON CHARINDEX((table2.real_email , table1.friend_email) > 0)I don't know anything about TSQL, so that could be off too. Let me know of if I can be unhelpful again.
Anthony
Perfect, this worked: SELECT * FROM table1 tb1 JOIN table2 tb2 ON CHARINDEX(tb1.real_email, tb2.friend_email) > 0
iamgoat
+1  A: 

You should be able to use the LIKE keyword depending on how consistent the pattern is for the "friendly" email addresses.

SELECT
     T1.nonfriendly_email_address,
     T2.friendly_email_address
FROM
     My_Table T1
INNER JOIN My_Table T2 ON
     T2.friendly_email_address LIKE '%<' + T1.nonfriendly_email_address + '>'
Tom H.