<?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>Never Say Never &#187; charindex</title>
	<atom:link href="http://phelabaum.com/archive/tag/charindex/feed/" rel="self" type="application/rss+xml" />
	<link>http://phelabaum.com</link>
	<description>MS SQL Server Development</description>
	<lastBuildDate>Thu, 29 Sep 2011 02:20:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>String Manipulation &#8211; CHARINDEX()</title>
		<link>http://phelabaum.com/archive/2009/12/string-manipulation-charindex/</link>
		<comments>http://phelabaum.com/archive/2009/12/string-manipulation-charindex/#comments</comments>
		<pubDate>Sat, 19 Dec 2009 18:34:22 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[charindex]]></category>
		<category><![CDATA[String Manipulation]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2009/12/string-manipulation-charindex/</guid>
		<description><![CDATA[I see charindex used quite commonly in string manipulation.&#160; What I rarely see used is the optional third parameter.&#160; Here I will explain how to use it, a situation where it can be useful and something to be careful of. FROM BOL: Syntax CHARINDEX ( expression1 ,expression2 [ , start_location ] ) Arguments expression1 Is [...]]]></description>
			<content:encoded><![CDATA[<p>I see charindex used quite commonly in string manipulation.&#160; What I rarely see used is the optional third parameter.&#160; Here I will explain how to use it, a situation where it can be useful and something to be careful of.</p>
<blockquote><p><strong><font size="1">FROM BOL:          <br /></font><span style="font-size: small">Syntax</span></strong>       <br />CHARINDEX ( expression1 ,expression2 [ , start_location ] )</p>
<p><strong><span style="font-size: small">Arguments</span></strong>       <br /><strong>expression1        <br /></strong>Is a character expression that contains the sequence to be found. expression1 is limited to 8000 characters.</p>
<p><strong>expression2 </strong>      <br />Is a character expression to be searched.</p>
<p><strong>start_location</strong>       <br />Is an integer or bigint expression at which the search starts. If start_location is not specified, is a negative number, or is 0, the search starts at the beginning of expression2.</p>
</blockquote>
<p><strong>How it works      <br /></strong>Let&#8217;s say we have a string with a set of characters in it and you want to find&#160; what is in between them.&#160; Easy enough if they&#8217;re different characters like [] or () (You could still use this for that situation to ensure you didn&#8217;t get an ending char before your starting char, and the warning below is still valid).&#160; But what if they&#8217;re the same character such as &quot;&quot; or &#8211;?&#160; Here&#8217;s how to use the third parameter of charindex along with a substring to accomplish this.</p>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<pre class="csharpcode"><span class="rem">-- Create a sample string with a pair of quotes.  The goal is to get what is in between the quotes. </span>
<span class="kwrd">declare</span> @a <span class="kwrd">varchar</span>(50)
<span class="kwrd">SET</span> @a = <span class="str">'This is a &quot;test&quot; string.'</span> 

<span class="rem">-- Find the First &quot; in the string </span>
<span class="kwrd">SELECT</span> <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)
<span class="rem">-- Find the Second &quot; in the string by starting one character past the first one </span>
<span class="kwrd">SELECT</span> <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a,<span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1)
<span class="rem">-- Put it together </span>
<span class="kwrd">SELECT</span> <span class="kwrd"><span style="color: #ff00ff">SUBSTRING</span></span>(@a,
                <span class="rem">-- Start from first &quot; </span>
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a),
                <span class="rem">-- Position of the second &quot; minus the position of the first &quot; to find length. </span>
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a,<span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1) - <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a))</pre>
<p>
  <br />We&#8217;re almost there.&#160; If you run the code, you&#8217;ll notice that the result is &quot;test .&#160; Let&#8217;s trim off that extra &quot;. </p>
<p></p>
<pre class="csharpcode"><span class="rem">-- Clean it up </span>
<span class="kwrd">SELECT</span> <span class="kwrd"><span style="color: #ff00ff">SUBSTRING</span></span>(@a,
                <span class="rem">-- Add 1 to trim off beginning quote </span>
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1,
                <span class="rem">-- Subtract 1 to trim off ending quote                            </span>
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a,<span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1)-1 - <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a))</pre>
<p><!-- .csharpcode, .csharpcode pre { 	font-size: small; 	color: black; 	font-family: consolas, "Courier New", courier, monospace; 	background-color: #ffffff; 	/*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt  { 	background-color: #f4f4f4; 	width: 100%; 	margin: 0em; } .csharpcode .lnum { color: #606060; } --></p>
<p><strong>Caution </strong></p>
<p>This is really a caution of the substring function, not charindex, but it is something that will cause the above technique to fail (and error).&#160; The problem occurs when you do not find the characters that you&#8217;re looking for.&#160; In the above example, I was looking for two &quot;&#8217;s.&#160; If they don&#8217;t exist, the code will error out.&#160; If no quotes are present, it works up until the cleanup portion.&#160; If only one &quot; is present, it fails at the first substring.&#160; You will receive this error:</p>
<p><span style="color: #ff0000">Msg 537, Level 16, State 2, Line 20</span> </p>
<p><font color="#ff0000">Invalid length parameter passed to the LEFT or SUBSTRING function.</font></p>
</p>
<p>The reason for this is that it attempts to pass a negative length to the substring function, which isn&#8217;t allowed.&#160; To safeguard against this, you can use a case statement like the following:</p>
<pre class="csharpcode"><span class="kwrd">DECLARE</span> @a <span class="kwrd">varchar</span>(50)
<span class="kwrd">SET</span> @a = <span class="str">'This is a &quot;fail  string.'</span> 

                <span class="rem">-- Test for a second &quot; before attempting the substring. </span>
<span class="kwrd">SELECT</span> <span class="kwrd">CASE</span> <span class="kwrd">WHEN</span> <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a,<span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1)-1 - <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)) &gt; 0 <span class="kwrd">THEN</span>
        <span class="kwrd"><span style="color: #ff00ff">SUBSTRING</span></span>(@a,
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1,
                <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a,<span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a)+1)-1 - <span style="color: #ff00ff">CHARINDEX</span>(<span class="str">'&quot;'</span>,@a))
            <span class="kwrd">ELSE</span> <span class="str">'Not Found'</span>
            <span class="kwrd">End</span></pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2009/12/string-manipulation-charindex/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

