<?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>tim laqua dot com &#187; string parsing</title>
	<atom:link href="http://timlaqua.com/tag/string-parsing/feed/" rel="self" type="application/rss+xml" />
	<link>http://timlaqua.com</link>
	<description>Thoughts and Code from Tim Laqua</description>
	<lastBuildDate>Sun, 09 May 2010 15:25:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Parsing QueryString (GET) Variables From a URL using T-SQL</title>
		<link>http://timlaqua.com/2009/05/parsing-querystring-get-variables-from-a-url-using-t-sql/</link>
		<comments>http://timlaqua.com/2009/05/parsing-querystring-get-variables-from-a-url-using-t-sql/#comments</comments>
		<pubDate>Sat, 23 May 2009 14:59:33 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[string parsing]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[table valued function]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=216</guid>
		<description><![CDATA[First of all - I know, don't do this. The application that put the URL in the column in the first place is MUCH better at handling URLs and ideally, you would just add columns for the GET variables you're after and have the application put those in. Parsing them out after the fact from [...]]]></description>
			<content:encoded><![CDATA[<p>First of all - I know, don't do this.  The application that put the URL in the column in the first place is MUCH better at handling URLs and ideally, you would just add columns for the GET variables you're after and have the application put those in.  Parsing them out after the fact from a VARCHAR field is insane.</p>
<p>So now we're here and we need to do that thing that we said we shouldn't do.  My approach is to use a Table Valued Function that only returns one column - the value of the variable or NULL if it can't find the variable in the querystring.<span id="more-216"></span></p>
<p><em>Example</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>
	a.<span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span>,
	b.<span style="color: #808080;">&#91;</span>GetVariableValue<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Foo<span style="color: #808080;">&#93;</span>,
	c.<span style="color: #808080;">&#91;</span>GetVariableValue<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Bar<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span>
	<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SourceTable<span style="color: #808080;">&#93;</span> a
	<span style="color: #808080;">CROSS</span> APPLY <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_UrlParseGetVariable_F01<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>URL<span style="color: #808080;">&#93;</span>, <span style="color: #FF0000;">'foo'</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> b
	<span style="color: #808080;">CROSS</span> APPLY <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_UrlParseGetVariable_F01<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>URL<span style="color: #808080;">&#93;</span>, <span style="color: #FF0000;">'bar'</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> c</pre></div></div>

<p><em>Table Valued Function</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/*************************************************************************************
***
*** Procedure:  		[Utility_UrlParseGetVariable_F01]
*** Purpose:		    Attempts to parse a given GET variable out of a URL string
***				
***			
*** Author:		      tl
*** Date Created:	  2009-05-22
*** 
*** Revision History
*** Date		Author			Description
*** 2009-05-22	tl				Created
*************************************************************************************/</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">FUNCTION</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_UrlParseGetVariable_F01<span style="color: #808080;">&#93;</span> 
<span style="color: #808080;">&#40;</span>
	@Url <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2048</span><span style="color: #808080;">&#41;</span>,
	@VariableName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">RETURNS</span> @GetVariable <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>GetVariableValue<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
	<span style="color: #0000FF;">DECLARE</span> @<span style="color: #FF00FF;">SUBSTRING</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2048</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">DECLARE</span> @variableValue <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
&nbsp;
	<span style="color: #0000FF;">SET</span> @VariableName <span style="color: #808080;">=</span> @VariableName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'='</span>
&nbsp;
	<span style="color: #008080;">-- Case Insensitive - munge both inputs</span>
	<span style="color: #0000FF;">SET</span> @VariableName <span style="color: #808080;">=</span> <span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>@VariableName<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">SET</span> @Url <span style="color: #808080;">=</span> <span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>@Url<span style="color: #808080;">&#41;</span>
&nbsp;
	<span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span>@VariableName,@Url<span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">BEGIN</span>
		<span style="color: #0000FF;">SET</span> @<span style="color: #FF00FF;">SUBSTRING</span> <span style="color: #808080;">=</span> <span style="color: #0000FF;">RIGHT</span><span style="color: #808080;">&#40;</span>@Url, <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>@Url<span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span>@VariableName,@Url<span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>@VariableName<span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'&amp;'</span>, @<span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
			<span style="color: #0000FF;">SET</span> @variableValue <span style="color: #808080;">=</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">LEFT</span><span style="color: #808080;">&#40;</span>@<span style="color: #FF00FF;">SUBSTRING</span>, <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'&amp;'</span>, @<span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">ELSE</span>
			<span style="color: #0000FF;">SET</span> @variableValue <span style="color: #808080;">=</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@<span style="color: #FF00FF;">SUBSTRING</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
&nbsp;
		<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @GetVariable <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>GetVariableValue<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>@variableValue<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">END</span>
	<span style="color: #0000FF;">ELSE</span>
		<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @GetVariable <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>GetVariableValue<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">NULL</span><span style="color: #808080;">&#41;</span>
&nbsp;
	<span style="color: #0000FF;">RETURN</span>
<span style="color: #0000FF;">END</span></pre></div></div>

<p><em>Issues</em></p>
<ul>
<li>The current TVF munges everything to lower case - the return is lower case as well.  This works fine for me as I was parsing out INT values anyway (so I use ISNULL(...,0) inside a cast) for the actual value.</li>
<li>The way it's written, if you look for the variable 'bar' and the querystring actually has a variable called 'foobar' before it - this function will take the value of 'foobar' and return it (because it's just doing a CHARINDEX for 'bar=' in the querystring</li>
</ul>
<p><em>Why not RegEx via CLR?</em><br />
Because one of the DBAs asked me not to <img src='http://timlaqua.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />   RegEx via CLR is a cleaner and more accurate way to do it.  Better yet, you could use CLR and create a Uri object, then use <code>HttpUtility.ParseQueryString(uri.Query).Name('bar')</code> to get the appropriate value.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/05/parsing-querystring-get-variables-from-a-url-using-t-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
