<?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>Simon Whatley &#187; user defined function</title>
	<atom:link href="http://www.simonwhatley.co.uk/tag/user-defined-function/feed" rel="self" type="application/rss+xml" />
	<link>http://www.simonwhatley.co.uk</link>
	<description>The opposite of every great idea is another great idea</description>
	<lastBuildDate>Wed, 02 Nov 2011 09:28:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>SQL User-Defined Function: ListToTable</title>
		<link>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable</link>
		<comments>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable#comments</comments>
		<pubDate>Mon, 22 Sep 2008 10:58:30 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sub-routine]]></category>
		<category><![CDATA[subroutine]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[tabular]]></category>
		<category><![CDATA[UDF]]></category>
		<category><![CDATA[user defined function]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=426</guid>
		<description><![CDATA[A common need in SQL is the ability to iterate over a list as if it were an array. In SQL it is not possible to declare arrays, unlike other programming languages such as ColdFusion, ActionScript and Java. Fortunately, there is a way around this problem: use a User-Defined Functions (UDFs) to create a tabular version of the data. Arrays are, after all, essentially tabular data (at their simplest, one dimension level).]]></description>
			<content:encoded><![CDATA[<p>A common need in <abbr title="Structured Query Language">SQL</abbr> is the ability to iterate over a list as if it were an <a href="http://en.wikipedia.org/wiki/Array" title="Wikipedia: Array" target="_blank" rel="nofollow">array</a>. In <abbr title="Structured Query Language">SQL</abbr> it is not possible to declare arrays, unlike other programming languages such as ColdFusion, ActionScript and Java. Fortunately, there is a way around this problem: use a <a href="http://en.wikipedia.org/wiki/User-defined_function" title="Wikipedia: User-Defined Functions" target="_blank" rel="nofollow">User-Defined Functions</a> (<abbr title="User Defined Functions">UDF</abbr>s) to create a tabular version of the data. Arrays are, after all, essentially tabular data (at their simplest, one dimension level).</p>
<p>A User-Defined Function, is a function provided by the user of a program or environment. In <abbr title="Structured Query Language">SQL</abbr> databases, a user-defined function provides a mechanism for extending the functionality of the database server by adding a function that can be evaluated in <abbr title="Structured Query Language">SQL</abbr> statements.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>The Function Code</h3>
<p>Below is the complete function definition:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>CREATE</span> <<span style="color: #0000ff;">|</span>/1/>FUNCTION</span> dbo.udf_ListToTable
<span style="color: #66cc66;">&#40;</span>
	@LIST 		<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span>,
	@DELIMITER 	<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">=</span> <span style="color: #ff0000;">','</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>RETURNS</span> @ListTable <<span style="color: #0000ff;">|</span>/1/>TABLE</span>
<span style="color: #66cc66;">&#40;</span>
	Item <<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>AS</span>
<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @LenDel 	<<span style="color: #0000ff;">|</span>/2/>INT</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @Pos 		<<span style="color: #0000ff;">|</span>/2/>INT</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span> @Item 		<<span style="color: #0000ff;">|</span>/2/>NVARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<span style="color: #808080;">--Get the length of the delimiter, use hack to get around LEN(' ') = 0 issue</span>
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @LenDel <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span>@DELIMITER <span style="color: #0000ff;">+</span> <span style="color: #ff0000;">'|'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">-</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @Pos <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">CHARINDEX</span><span style="color: #66cc66;">&#40;</span>@DELIMITER, @LIST<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>WHILE</span> @Pos <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
	<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
		<span style="color: #808080;">--Get the item</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @Item <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@LIST, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, @Pos-<<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Add it to the table (if not empty string)</span>
		<<span style="color: #0000ff;">|</span>/1/>IF</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@Item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
			<<span style="color: #0000ff;">|</span>/1/>INSERT</span> @ListTable <span style="color: #66cc66;">&#40;</span>Item<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@Item<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Remove the item from the list</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @LIST <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">STUFF</span><span style="color: #66cc66;">&#40;</span>@LIST, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, @Pos+@LenDel-<<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>, <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>
		<span style="color: #808080;">--Get the position of the next delimiter</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span> @Pos <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">CHARINDEX</span><span style="color: #66cc66;">&#40;</span>@DELIMITER, @LIST<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>END</span>
&nbsp;
	<span style="color: #808080;">--Add the last item to the table (if not empty string)</span>
	<<span style="color: #0000ff;">|</span>/1/>IF</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@LIST<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
		<<span style="color: #0000ff;">|</span>/1/>INSERT</span> @ListTable <span style="color: #66cc66;">&#40;</span>Item<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">LTRIM</span><span style="color: #66cc66;">&#40;</span>@LIST<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>RETURN</span>
<<span style="color: #0000ff;">|</span>/1/>END</span>
<<span style="color: #0000ff;">|</span>/1/>GO</span></pre></div></div>

<p>The function simply loops over the list passed into the function. Each list item is then inserted into the variable named <code>@ListTable</code>, which is of type <code>TABLE</code>. The <code>@ListTable</code> variable is then returned out of the function and can be handled the same as any other table.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>The Function In Use</h3>
<p>A simple demonstration is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>INSERT</span> <<span style="color: #0000ff;">|</span>/1/>INTO</span> tableName <span style="color: #66cc66;">&#40;</span>column1, column2, column3, column4<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>SELECT</span> @variable1, @variable2, myTable.item, <span style="color: #ff00ff;">GETDATE</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>FROM</span> dbo.udf_ListToTable<span style="color: #66cc66;">&#40;</span>@list,<span style="color: #ff0000;">','</span><span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>AS</span> myTable</pre></div></div>

<p>In this example, we insert the same information (<code>@variable1</code>, <code>@variable2</code>) for every instance of an item found in <code>myTable</code>.</p>
<p>This is useful, for example, if you want to apply a setting to a group of users. The group of users could be contained in a list that needs to be parsed as a table, whilst the individual setting details are contained in the other variables.</p>
<h3>Download the Code</h3>
<p><a href="/examples/sql/functions/udf_ListToTable.txt" title="Download the code">Download the code</a>, rename the file to .sql and run on your database instance. You will then be able to reference the function in your Stored Procedures.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/sql-user-defined-function-listtotable/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL User-Defined Function: ReplaceChars</title>
		<link>http://www.simonwhatley.co.uk/sql-user-defined-function-replacechars</link>
		<comments>http://www.simonwhatley.co.uk/sql-user-defined-function-replacechars#comments</comments>
		<pubDate>Fri, 19 Sep 2008 14:46:13 +0000</pubDate>
		<dc:creator>Simon</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database server]]></category>
		<category><![CDATA[extend]]></category>
		<category><![CDATA[fairly straight forward]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sub-routine]]></category>
		<category><![CDATA[subroutine]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[UDF]]></category>
		<category><![CDATA[user defined function]]></category>

		<guid isPermaLink="false">http://www.simonwhatley.co.uk/?p=425</guid>
		<description><![CDATA[The SQL Replace function enables us to look for a certain character phrase in a string and replace it with another character phrase. The updated string is then returned by the function.]]></description>
			<content:encoded><![CDATA[<p>The <abbr title="Structured Query Language">SQL</abbr> <code>REPLACE</code> function enables us to look for a certain character phrase in a string and replace it with another character phrase. The updated string is then returned by the function.</p>
<p>The syntax for this string function is the same for <abbr title="Structured Query Language">SQL</abbr> Server, Oracle and Microsoft Access. The syntax is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringToLookIn, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>The syntax is fairly straight forward, the <em>stringToMatch</em> parameter is the character phrase that we want to replace, the <em>replacementsString</em> is the character phrase that will replace any occurence of the stringToMatch parameter. If the stringToMatch phrase occurs more than once in the string, then all instances of the phrase will be replaced with the replacement string. If no matches were found then the string is returned unaltered.</p>
<p>If we want to match multiple items, we need to nest the <code>REPLACE</code> function:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringToLookIn, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span>, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>or set the replaced string into a new variable multiple times:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">stringReturned <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringToLookIn, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span>
stringReturned <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>stringReturned, stringToMatch, replacementsString<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This is far from ideal, especially the more strings there are to be matched. This is where <a href="http://en.wikipedia.org/wiki/User-defined_function" title="Wikipedia: User-Defined Functions" target="_blank" rel="nofollow">User-Defined Functions</a> (<abbr title="User Defined Functions">UDF</abbr>s) can provide the answer.</p>
<p>A User-Defined Function, is a function provided by the user of a program or environment. In <abbr title="Structured Query Language">SQL</abbr> databases, a user-defined function provides a mechanism for extending the functionality of the database server by adding a function that can be evaluated in <abbr title="Structured Query Language">SQL</abbr> statements.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h3>The Function Code</h3>
<p>Below is the complete function definition:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>CREATE</span> <<span style="color: #0000ff;">|</span>/1/>FUNCTION</span> dbo.udf_ReplaceChars
<span style="color: #66cc66;">&#40;</span>
@ReplaceList		<<span style="color: #0000ff;">|</span>/2/>VARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span>,
@String			<<span style="color: #0000ff;">|</span>/2/>VARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>RETURNS</span> <<span style="color: #0000ff;">|</span>/2/>VARCHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>AS</span>
<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
	<<span style="color: #0000ff;">|</span>/1/>DECLARE</span>	@<<span style="color: #0000ff;">|</span>/2/>CHAR</span>		<<span style="color: #0000ff;">|</span>/2/>CHAR</span><span style="color: #66cc66;">&#40;</span><<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>,
		@Loop		<<span style="color: #0000ff;">|</span>/2/>INT</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>SET</span> @Loop  <span style="color: #0000ff;">=</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">0</span>
	<<span style="color: #0000ff;">|</span>/1/>WHILE</span> @Loop <span style="color: #66cc66;">&lt;</span> <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">LEN</span><span style="color: #66cc66;">&#40;</span>@ReplaceList<span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>BEGIN</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span>	@Loop <span style="color: #0000ff;">=</span> @Loop <span style="color: #0000ff;">+</span> <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span>	@<<span style="color: #0000ff;">|</span>/2/>CHAR</span> <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@ReplaceList, @Loop, <<span style="color: #0000ff;">|</span> style<span style="color: #66cc66;">=</span>"color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
		<<span style="color: #0000ff;">|</span>/1/>SET</span>	@String <span style="color: #0000ff;">=</span> <span style="color: #ff00ff;">REPLACE</span><span style="color: #66cc66;">&#40;</span>@String, @<<span style="color: #0000ff;">|</span>/2/>CHAR</span>, <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>
	<<span style="color: #0000ff;">|</span>/1/>END</span>
&nbsp;
	<<span style="color: #0000ff;">|</span>/1/>RETURN</span>		@String
&nbsp;
<<span style="color: #0000ff;">|</span>/1/>END</span>
<<span style="color: #0000ff;">|</span>/1/>GO</span></pre></div></div>

<p>The function simply loops over the replace list, finding each instance of the list item in the string in which we want to replace items. The new string is then returned out of the function.</p>
<h3>The Function In Use</h3>
<p>A very simple use of the replace function could be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>SELECT</span> dbo.udf_ReplaceChars<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'=,/,&lt;,&gt;,@,~,#'</span>, columnName<span style="color: #66cc66;">&#41;</span> <<span style="color: #0000ff;">|</span>/1/>AS</span> newColumn, columnName
<<span style="color: #0000ff;">|</span>/1/>FROM</span> tableName</pre></div></div>

<p>The function is not restricted to <code>SELECT</code> statements. Below is an example of an <code>UPDATE</code> statement utilising a variable:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><<span style="color: #0000ff;">|</span>/1/>UPDATE</span> tableName
<<span style="color: #0000ff;">|</span>/1/>SET</span> columnName <span style="color: #0000ff;">=</span> dbo.udf_ReplaceChars<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'=,/,&lt;,&gt;,@,~,#'</span>, @variableName<span style="color: #66cc66;">&#41;</span>
<<span style="color: #0000ff;">|</span>/1/>WHERE</span> idName <span style="color: #0000ff;">=</span> @myId</pre></div></div>

<h3>Download the Code</h3>
<p><a href="/examples/sql/functions/udf_ReplaceChars.txt" title="Download the code">Download the code</a>, rename the file to .sql and run on your database instance. You will then be able to reference the function in your Stored Procedures.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6475233631580417";
/* 468x60 Basic */
google_ad_slot = "7117418273";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.simonwhatley.co.uk/sql-user-defined-function-replacechars/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

