<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>tmllr &#187; Great Tricks</title>
	<atom:link href="http://www.tmllr.com/category/great-tricks/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tmllr.com</link>
	<description>Building. Tracking. Optimizing. Inspiring.</description>
	<lastBuildDate>Wed, 19 Oct 2011 18:01:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Great Trick: Extracting Domains from Email Addresses in Excel and SQL</title>
		<link>http://www.tmllr.com/2010/02/great-trick-extracting-domains-from-email-addresses-in-excel-and-sql/</link>
		<comments>http://www.tmllr.com/2010/02/great-trick-extracting-domains-from-email-addresses-in-excel-and-sql/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 12:00:37 +0000</pubDate>
		<dc:creator>Tom Miller</dc:creator>
				<category><![CDATA[Great Tricks]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.tomsanalytics.com/?p=234</guid>
		<description><![CDATA[By using functions in Excel and SQL that return the text from specific locations within a string combined with ones that can isolate the location of the &#8220;@&#8221; character in every email address, you can easily extract domain names from lists of email addresses. EXCEL: The base function for this is RIGHT.  RIGHT gets passed [...]]]></description>
			<content:encoded><![CDATA[<p>By using functions in Excel and SQL that return the text from specific locations within a string combined with ones that can isolate the location of the &#8220;@&#8221; character in every email address, you can easily extract domain names from lists of email addresses.</p>
<h3><strong>EXCEL:</strong></h3>
<p>The base function for this is <strong>RIGHT</strong>.  <strong>RIGHT</strong> gets passed two arguments, <em>text</em>, which is the text being parsed, and <em>num_chars</em>, which is the number of characters returned by the function.  <strong>RIGHT</strong> takes the form in Excel of <strong>RIGHT(text,[num_chars])</strong>.</p>
<p>The <em>text</em> argument is obvious; it is the text of the email address to be parsed.</p>
<p>The <em>num_chars</em> argument is determined using a combination of two other functions, <strong>LEN</strong> and <strong>FIND</strong>.  We use <strong>LEN</strong> to determine the length of the overall email address and subtract the position of the @ operator, determined using <strong>FIND</strong>.  The resulting differencewill return the length of the domain portion of the email address.</p>
<p><strong>LEN(<em>text</em>)</strong> returns the number of characters in the string.</p>
<p><strong>FIND(<em>find_text</em>,<em>within_text</em>,<em>start_num</em>)</strong> returns the postion of the <em>find_text</em> within the <em>within_text</em>.  <em>start_num</em>, which we won&#8217;t use here, is a way to start selecting text after a certain number of characters.</p>
<p>To put this all together, let&#8217;s put my email address in cell A1, place our derived function into the B1, and derive the result.</p>
<blockquote><p>
=RIGHT(A1,((LEN(A1)-FIND(&#8220;@&#8221;,A1)))) <em>calculates to:</em><br />
=RIGHT(A1,(21-4)) <em>to:</em><br />
=RIGHT(A1,17) <em>to:</em><br />
=tomsanalytics.com</p></blockquote>
<p><img class="size-full wp-image-235 alignnone" title="excel" src="http://www.tmllr.com/wp-content/uploads/2010/02/excel.png" alt="" width="523" height="103" /></p>
<h3><strong>SQL:</strong></h3>
<p>SQL is a little bit trickier, as there isn&#8217;t a right-to-left text selection function, instead we are going to use a left-to-right function, <strong><a href="http://msdn.microsoft.com/en-us/library/ms187748.aspx" target="_blank">SUBSTRING</a></strong>.</p>
<p><strong>SUBSTRING</strong> returns a string of text based on definition passed to the function.  <strong>SUBSTRING</strong> takes three arguments, <em>value_expression</em>, which is the text being parsed, <em>start_expression</em>, which is the starting character of the returned string, and <em>length_expression</em>, which is the number of characters returned, starting with the start_expression.  This function in SQL looks like this: <strong>SUBSTRING(<em>value_expression</em>,<em>start_expression</em>,<em>length_expression</em>)<span style="font-weight: normal;">.</span></strong></p>
<p>In this case our email address is the <em>value_expression</em>.  The character following the &#8220;@&#8221; symbol is the <em>start_expression</em>, with the length of the remaining string being the <em>length_expression</em>.</p>
<p>To determine <em>start_expression</em>, we deploy another SQL function, <strong><a href="http://msdn.microsoft.com/en-us/library/ms186323.aspx" target="_blank">CHARINDEX</a></strong>, which works exactly like <strong>FIND</strong> in Excel.  Using SQL&#8217;s version of <strong>LEN</strong> and the same math, we can determine <em>length_expression</em>.</p>
<p>Putting it all together, let&#8217;s assume a table named email_addresses with a column named email:</p>
<blockquote><p>
SELECT SUBSTRING(email, CHARINDEX(&#8216;@&#8217;, email) + 1, LEN(email) &#8211; CHARINDEX(&#8216;@&#8217;, email) + 1) AS domain_name<br />
FROM email_addresses<br />
WHERE email like &#8216;%@%&#8217;</p></blockquote>
<p>The WHERE clause is in there to prevent malformed email addresses from crashing the <strong>CHARINDEX</strong> function.</p>
<p>The math works the same way as the Excel, except that you have to remember that we are working from the left, so the need arises to add one to the character counter in the <strong>CHARINDEX</strong> function.</p>
<p>Simple and powerful.  I hope this is useful to someone &#8211; the inspiration for this post came from <a href="http://chandoo.org/wp/2010/01/19/usernames-from-email-formulas/" target="_blank">this post at Chandoo.org</a>.  <a href="http://www.chandoo.org" target="_blank">Pointy Haird Dilbert</a> is easily my favorite as well as one of the most useful and entertaining Excel blogs out there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tmllr.com/2010/02/great-trick-extracting-domains-from-email-addresses-in-excel-and-sql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

