<?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>We Heart Code &#187; sql</title>
	<atom:link href="http://www.weheartcode.com/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.weheartcode.com</link>
	<description>A discourse on programming</description>
	<lastBuildDate>Sun, 17 Jan 2010 02:16:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Oracle Delete Taking a Long Time</title>
		<link>http://www.weheartcode.com/2010/01/16/oracle-delete-taking-a-long-time/</link>
		<comments>http://www.weheartcode.com/2010/01/16/oracle-delete-taking-a-long-time/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 02:16:15 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[oracle]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/?p=62</guid>
		<description><![CDATA[ It may look like your delete statement is hung, but ff your table has foreign keys in another table without indexes on that column Oracle will full table scan that other table for every row that you&#8217;re deleting. Hopefully this saves someone some time.
Bookmark to:
]]></description>
			<content:encoded><![CDATA[<p> It may look like your delete statement is hung, but ff your table has foreign keys in another table without indexes on that column Oracle will full table scan that other table for every row that you&#8217;re deleting. Hopefully this saves someone some time.</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2010/01/16/oracle-delete-taking-a-long-time/&amp;title=Oracle+Delete+Taking+a+Long+Time" title="Add 'Oracle Delete Taking a Long Time' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Oracle Delete Taking a Long Time' to Del.icio.us" alt="Add 'Oracle Delete Taking a Long Time' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2010/01/16/oracle-delete-taking-a-long-time/&amp;title=Oracle+Delete+Taking+a+Long+Time" title="Add 'Oracle Delete Taking a Long Time' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Oracle Delete Taking a Long Time' to digg" alt="Add 'Oracle Delete Taking a Long Time' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Oracle+Delete+Taking+a+Long+Time&amp;u=http://www.weheartcode.com/2010/01/16/oracle-delete-taking-a-long-time/" title="Add 'Oracle Delete Taking a Long Time' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Oracle Delete Taking a Long Time' to FURL" alt="Add 'Oracle Delete Taking a Long Time' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2010/01/16/oracle-delete-taking-a-long-time/&amp;title=Oracle+Delete+Taking+a+Long+Time" title="Add 'Oracle Delete Taking a Long Time' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Oracle Delete Taking a Long Time' to reddit" alt="Add 'Oracle Delete Taking a Long Time' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2010/01/16/oracle-delete-taking-a-long-time/" title="Add 'Oracle Delete Taking a Long Time' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Oracle Delete Taking a Long Time' to Technorati" alt="Add 'Oracle Delete Taking a Long Time' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2010/01/16/oracle-delete-taking-a-long-time/&amp;title=Oracle+Delete+Taking+a+Long+Time" title="Add 'Oracle Delete Taking a Long Time' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Oracle Delete Taking a Long Time' to Google Bookmarks" alt="Add 'Oracle Delete Taking a Long Time' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2010/01/16/oracle-delete-taking-a-long-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Luhn Digit Generation Oracle PL/SQL Implementation</title>
		<link>http://www.weheartcode.com/2009/10/06/luhn-digit-generation-oracle-plsql-implementation/</link>
		<comments>http://www.weheartcode.com/2009/10/06/luhn-digit-generation-oracle-plsql-implementation/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 11:27:06 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/?p=51</guid>
		<description><![CDATA[Here's how to generate a Luhn Mod-10 Check Digit. It's implemented as a function in oracle PL/SQL. I spent 2 hours looking for this online, then decided to implement it myself since it's not that hard. Anyway, I thought I'd post it here for the next person trolling google for it. This is useful for [...]]]></description>
			<content:encoded><![CDATA[<p>Here's how to generate a Luhn Mod-10 Check Digit. It's implemented as a function in oracle PL/SQL. I spent 2 hours looking for this online, then decided to implement it myself since it's not that hard. Anyway, I thought I'd post it here for the next person trolling google for it. This is useful for generating credit card numbers or any other place you need a check digit.</p>
<p>This algorithm works by doing the same thing as the validation algorithm, but shifted over one character (the equivalent of adding a zero to a number, generating the sum &#038; subtracting 10).</p>
<p><strong>*EDIT - there was a bug in this code, i just fixed it on 12/4/2009, the old code returned 10 instead of zero. It also was only working on even numbers, sorry about that! </strong></p>
<div class="syntax_hilite">
<div id="sql-2">
<div class="sql"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #993333; font-weight: bold;">REPLACE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> generate_luhn <span style="color:#006600; font-weight:bold;">&#40;</span>togen <span style="color: #993333; font-weight: bold;">IN</span> varchar<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">RETURN</span> number<br />
<span style="color: #993333; font-weight: bold;">IS</span><br />
&nbsp; &nbsp; curval NUMBER := <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; total NUMBER := <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; everyother NUMBER := <span style="color: #cc66cc;">1</span>;</p>
<p>BEGIN<br />
&nbsp; <span style="color: #993333; font-weight: bold;">IF</span><span style="color:#006600; font-weight:bold;">&#40;</span><br />
&nbsp; &nbsp;togen <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#41;</span> then<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #cc66cc;">0</span>;<br />
&nbsp; end <span style="color: #993333; font-weight: bold;">IF</span>;</p>
<p>&nbsp; <br />
&nbsp; <span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> reverse <span style="color: #cc66cc;">1</span> .. LENGTH<span style="color:#006600; font-weight:bold;">&#40;</span>togen<span style="color:#006600; font-weight:bold;">&#41;</span> loop<br />
&nbsp; &nbsp; curval:=SUBSTR<span style="color:#006600; font-weight:bold;">&#40;</span>togen, i, <span style="color: #cc66cc;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">IF</span> everyother = <span style="color: #cc66cc;">1</span> then<br />
&nbsp; &nbsp; &nbsp; &nbsp;everyother := <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; &nbsp; curval := curval * <span style="color: #cc66cc;">2</span>;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">IF</span><span style="color:#006600; font-weight:bold;">&#40;</span>curval&gt; <span style="color: #cc66cc;">9</span><span style="color:#006600; font-weight:bold;">&#41;</span> then<br />
&nbsp; &nbsp; &nbsp; &nbsp; curval:=curval-<span style="color: #cc66cc;">9</span>;<br />
&nbsp; &nbsp; &nbsp; end <span style="color: #993333; font-weight: bold;">IF</span>;<br />
&nbsp; &nbsp; &nbsp;else<br />
&nbsp; &nbsp; &nbsp; &nbsp;everyother := <span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp; end <span style="color: #993333; font-weight: bold;">IF</span>;</p>
<p>&nbsp; &nbsp;<br />
&nbsp; &nbsp; total := total + curval;<br />
&nbsp; end loop;<br />
&nbsp; <span style="color: #993333; font-weight: bold;">IF</span> MOD<span style="color:#006600; font-weight:bold;">&#40;</span>total, <span style="color: #cc66cc;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span> = <span style="color: #cc66cc;">0</span> THEN<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #cc66cc;">0</span>;<br />
&nbsp; &nbsp; ELSE<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">RETURN</span> <span style="color: #cc66cc;">10</span> - MOD<span style="color:#006600; font-weight:bold;">&#40;</span>total, <span style="color: #cc66cc;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>;<br />
&nbsp; &nbsp; END <span style="color: #993333; font-weight: bold;">IF</span>;<br />
END generate_luhn;<br />
/</div>
</div>
</div>
<p></p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2009/10/06/luhn-digit-generation-oracle-plsql-implementation/&amp;title=Luhn+Digit+Generation+Oracle+PL%2FSQL+Implementation" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to Del.icio.us" alt="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2009/10/06/luhn-digit-generation-oracle-plsql-implementation/&amp;title=Luhn+Digit+Generation+Oracle+PL%2FSQL+Implementation" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to digg" alt="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Luhn+Digit+Generation+Oracle+PL%2FSQL+Implementation&amp;u=http://www.weheartcode.com/2009/10/06/luhn-digit-generation-oracle-plsql-implementation/" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to FURL" alt="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2009/10/06/luhn-digit-generation-oracle-plsql-implementation/&amp;title=Luhn+Digit+Generation+Oracle+PL%2FSQL+Implementation" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to reddit" alt="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2009/10/06/luhn-digit-generation-oracle-plsql-implementation/" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to Technorati" alt="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2009/10/06/luhn-digit-generation-oracle-plsql-implementation/&amp;title=Luhn+Digit+Generation+Oracle+PL%2FSQL+Implementation" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to Google Bookmarks" alt="Add 'Luhn Digit Generation Oracle PL/SQL Implementation' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2009/10/06/luhn-digit-generation-oracle-plsql-implementation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Postgres select replace with regular expression</title>
		<link>http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/</link>
		<comments>http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/#comments</comments>
		<pubDate>Wed, 02 Jan 2008 21:22:50 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/</guid>
		<description><![CDATA[Postgres seriously kicks ass, here's a quick way to do a substring based on a regular expression. A Co-worker wanted to get the first word out of a column that contained a sentence. If we were using something lame like oracle we'd have to do gymnastics to do that.
It's a one-liner in postgres.


SELECT substring&#40;'this is [...]]]></description>
			<content:encoded><![CDATA[<p>Postgres seriously kicks ass, here's a quick way to do a substring based on a regular expression. A Co-worker wanted to get the first word out of a column that contained a sentence. If we were using something lame like oracle we'd have to do gymnastics to do that.</p>
<p>It's a one-liner in postgres.</p>
<div class="syntax_hilite">
<div id="sql-4">
<div class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> substring<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #ff0000;">'this is a sentence'</span> <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #ff0000;">'^(.+?)<span style="color: #000099; font-weight: bold;">\\</span>s.+$'</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</div>
<p></p>
<p>So we're selecting out the group that is a series of characters at the start of a line (column) before a space. Epic win.</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/&amp;title=Postgres+select+replace+with+regular+expression" title="Add 'Postgres select replace with regular expression' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Postgres select replace with regular expression' to Del.icio.us" alt="Add 'Postgres select replace with regular expression' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/&amp;title=Postgres+select+replace+with+regular+expression" title="Add 'Postgres select replace with regular expression' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Postgres select replace with regular expression' to digg" alt="Add 'Postgres select replace with regular expression' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Postgres+select+replace+with+regular+expression&amp;u=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/" title="Add 'Postgres select replace with regular expression' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Postgres select replace with regular expression' to FURL" alt="Add 'Postgres select replace with regular expression' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/&amp;title=Postgres+select+replace+with+regular+expression" title="Add 'Postgres select replace with regular expression' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Postgres select replace with regular expression' to reddit" alt="Add 'Postgres select replace with regular expression' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/" title="Add 'Postgres select replace with regular expression' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Postgres select replace with regular expression' to Technorati" alt="Add 'Postgres select replace with regular expression' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/&amp;title=Postgres+select+replace+with+regular+expression" title="Add 'Postgres select replace with regular expression' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Postgres select replace with regular expression' to Google Bookmarks" alt="Add 'Postgres select replace with regular expression' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2008/01/02/postgres-select-replace-with-regular-expression/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Randomizing Query Results in Oracle</title>
		<link>http://www.weheartcode.com/2007/06/20/randomizing-query-results-in-oracle/</link>
		<comments>http://www.weheartcode.com/2007/06/20/randomizing-query-results-in-oracle/#comments</comments>
		<pubDate>Wed, 20 Jun 2007 17:45:14 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[oracle]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2007/06/20/randomizing-query-results-in-oracle/</guid>
		<description><![CDATA[I was doing some work this afternoon, and I needed some sample id's from a database query, however all the data ended up being related to the same person because of the ordering of the results. There must be a better way, i want random results in my resultset.
Here's how (in oracle), order by a [...]]]></description>
			<content:encoded><![CDATA[<p>I was doing some work this afternoon, and I needed some sample id's from a database query, however all the data ended up being related to the same person because of the ordering of the results. There must be a better way, i want random results in my resultset.</p>
<p>Here's how (in oracle), order by a random value using <strong>dbms_random.value()</strong></p>
<div class="syntax_hilite">
<div id="sql-6">
<div class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> OURTABLE <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> dbms_random.value<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div>
</div>
</div>
<p></p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2007/06/20/randomizing-query-results-in-oracle/&amp;title=Randomizing+Query+Results+in+Oracle" title="Add 'Randomizing Query Results in Oracle' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Randomizing Query Results in Oracle' to Del.icio.us" alt="Add 'Randomizing Query Results in Oracle' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2007/06/20/randomizing-query-results-in-oracle/&amp;title=Randomizing+Query+Results+in+Oracle" title="Add 'Randomizing Query Results in Oracle' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Randomizing Query Results in Oracle' to digg" alt="Add 'Randomizing Query Results in Oracle' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Randomizing+Query+Results+in+Oracle&amp;u=http://www.weheartcode.com/2007/06/20/randomizing-query-results-in-oracle/" title="Add 'Randomizing Query Results in Oracle' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Randomizing Query Results in Oracle' to FURL" alt="Add 'Randomizing Query Results in Oracle' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2007/06/20/randomizing-query-results-in-oracle/&amp;title=Randomizing+Query+Results+in+Oracle" title="Add 'Randomizing Query Results in Oracle' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Randomizing Query Results in Oracle' to reddit" alt="Add 'Randomizing Query Results in Oracle' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2007/06/20/randomizing-query-results-in-oracle/" title="Add 'Randomizing Query Results in Oracle' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Randomizing Query Results in Oracle' to Technorati" alt="Add 'Randomizing Query Results in Oracle' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2007/06/20/randomizing-query-results-in-oracle/&amp;title=Randomizing+Query+Results+in+Oracle" title="Add 'Randomizing Query Results in Oracle' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Randomizing Query Results in Oracle' to Google Bookmarks" alt="Add 'Randomizing Query Results in Oracle' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2007/06/20/randomizing-query-results-in-oracle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessing main query aliases from subquery</title>
		<link>http://www.weheartcode.com/2007/03/16/accessing-main-query-aliases-from-subquery/</link>
		<comments>http://www.weheartcode.com/2007/03/16/accessing-main-query-aliases-from-subquery/#comments</comments>
		<pubDate>Fri, 16 Mar 2007 13:42:53 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[oracle]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2007/03/16/accessing-main-query-aliases-from-subquery/</guid>
		<description><![CDATA[I am stupid, I never fully understood Oracle Subqueries. Let me give you an example, I had a really big view where I wanted to access stats of child tables based on a parent table.
For example, let's call that table checks, I wanted to know how many responses were linked to each check. We have [...]]]></description>
			<content:encoded><![CDATA[<p>I am stupid, I never fully understood Oracle Subqueries. Let me give you an example, I had a really big view where I wanted to access stats of child tables based on a parent table.</p>
<p>For example, let's call that table checks, I wanted to know how many responses were linked to each check. We have a a one to many relationship between checks and responses.</p>
<p>So I wrote my original query something like this:</p>
<div class="syntax_hilite">
<div id="sql-9">
<div class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> c.check_id,cr.count_of_responses<br />
<span style="color: #993333; font-weight: bold;">FROM</span> c.checks<br />
<span style="color: #993333; font-weight: bold;">WHERE</span><br />
<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> count<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>,r.check_id&nbsp; <span style="color: #993333; font-weight: bold;">AS</span> count_of_responses <span style="color: #993333; font-weight: bold;">FROM</span> responses r <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> r.check_id<span style="color:#006600; font-weight:bold;">&#41;</span> cr <br />
<span style="color: #993333; font-weight: bold;">ON</span> cr.check_id = c.check_id</div>
</div>
</div>
<p></p>
<p>This works BUT, it produces a full tablescan on the child table unless you specify a check_id in the main query. This aint that useful if you want to say query on the count_of_responses for all checks. What if we could access the main query variables in the subquery? Then the database wouldn't have to full tablescan and join it all together.</p>
<p>Well you can if you do your subquery in the SELECT clause, the only catch is you can only select one thing. The good thing is it doesn't do full tablescans, it will use your foriegn key indexes!</p>
<p>So our rewritten query would look like this:</p>
<div class="syntax_hilite">
<div id="sql-10">
<div class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> c.check_id,<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> count<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> responses r <span style="color: #993333; font-weight: bold;">WHERE</span> r.check_id = c.check_id<span style="color:#006600; font-weight:bold;">&#41;</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> c.checks</div>
</div>
</div>
<p></p>
<p>Golden, now for each row it just joins using the index pulling back a single row from responses! </p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2007/03/16/accessing-main-query-aliases-from-subquery/&amp;title=Accessing+main+query+aliases+from+subquery" title="Add 'Accessing main query aliases from subquery' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Accessing main query aliases from subquery' to Del.icio.us" alt="Add 'Accessing main query aliases from subquery' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2007/03/16/accessing-main-query-aliases-from-subquery/&amp;title=Accessing+main+query+aliases+from+subquery" title="Add 'Accessing main query aliases from subquery' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Accessing main query aliases from subquery' to digg" alt="Add 'Accessing main query aliases from subquery' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Accessing+main+query+aliases+from+subquery&amp;u=http://www.weheartcode.com/2007/03/16/accessing-main-query-aliases-from-subquery/" title="Add 'Accessing main query aliases from subquery' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Accessing main query aliases from subquery' to FURL" alt="Add 'Accessing main query aliases from subquery' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2007/03/16/accessing-main-query-aliases-from-subquery/&amp;title=Accessing+main+query+aliases+from+subquery" title="Add 'Accessing main query aliases from subquery' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Accessing main query aliases from subquery' to reddit" alt="Add 'Accessing main query aliases from subquery' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2007/03/16/accessing-main-query-aliases-from-subquery/" title="Add 'Accessing main query aliases from subquery' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Accessing main query aliases from subquery' to Technorati" alt="Add 'Accessing main query aliases from subquery' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2007/03/16/accessing-main-query-aliases-from-subquery/&amp;title=Accessing+main+query+aliases+from+subquery" title="Add 'Accessing main query aliases from subquery' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Accessing main query aliases from subquery' to Google Bookmarks" alt="Add 'Accessing main query aliases from subquery' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2007/03/16/accessing-main-query-aliases-from-subquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Formatting Numbers in Oracle and Postgres</title>
		<link>http://www.weheartcode.com/2006/10/09/formatting-numbers-in-oracle/</link>
		<comments>http://www.weheartcode.com/2006/10/09/formatting-numbers-in-oracle/#comments</comments>
		<pubDate>Mon, 09 Oct 2006 12:29:06 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[oracle]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2006/10/09/formatting-numbers-in-oracle/</guid>
		<description><![CDATA[When dealing with large numbers in oracle sometimes it can be hard to read the results from a query with no formatting in the results. I know I have misread numbers from a query many times. Well, I finally got around to looking up how to format numbers in oracle. And who knew -- TO_CHAR [...]]]></description>
			<content:encoded><![CDATA[<p>When dealing with large numbers in oracle sometimes it can be hard to read the results from a query with no formatting in the results. I know I have misread numbers from a query many times. Well, I finally got around to looking up how to format numbers in oracle. And who knew -- TO_CHAR does it for us.</p>
<p>Example In Oracle:</p>
<div class="syntax_hilite">
<div id="sql-13">
<div class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> to_char<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;">5689731</span>,<span style="color: #ff0000;">'$9,999,999.00'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> mysalary <span style="color: #993333; font-weight: bold;">FROM</span> DUAL</div>
</div>
</div>
<p></p>
<p>Jason says this also works in Postgres!<br />
Example in Postgres:</p>
<div class="syntax_hilite">
<div id="sql-14">
<div class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> to_char<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #cc66cc;">5689731</span>, <span style="color: #ff0000;">'$9,999,999.00'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> mysalary</div>
</div>
</div>
<p>
returns</p>
<p>$5,689,731.00</p>
<!-- Social Bookmarks BEGIN --><div class="social_bookmark"><em>Bookmark to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://www.weheartcode.com/2006/10/09/formatting-numbers-in-oracle/&amp;title=Formatting+Numbers+in+Oracle+and+Postgres" title="Add 'Formatting Numbers in Oracle and Postgres' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Formatting Numbers in Oracle and Postgres' to Del.icio.us" alt="Add 'Formatting Numbers in Oracle and Postgres' to Del.icio.us" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://www.weheartcode.com/2006/10/09/formatting-numbers-in-oracle/&amp;title=Formatting+Numbers+in+Oracle+and+Postgres" title="Add 'Formatting Numbers in Oracle and Postgres' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Formatting Numbers in Oracle and Postgres' to digg" alt="Add 'Formatting Numbers in Oracle and Postgres' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://furl.net/storeIt.jsp?t=Formatting+Numbers+in+Oracle+and+Postgres&amp;u=http://www.weheartcode.com/2006/10/09/formatting-numbers-in-oracle/" title="Add 'Formatting Numbers in Oracle and Postgres' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Formatting Numbers in Oracle and Postgres' to FURL" alt="Add 'Formatting Numbers in Oracle and Postgres' to FURL" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://www.weheartcode.com/2006/10/09/formatting-numbers-in-oracle/&amp;title=Formatting+Numbers+in+Oracle+and+Postgres" title="Add 'Formatting Numbers in Oracle and Postgres' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Formatting Numbers in Oracle and Postgres' to reddit" alt="Add 'Formatting Numbers in Oracle and Postgres' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://www.weheartcode.com/2006/10/09/formatting-numbers-in-oracle/" title="Add 'Formatting Numbers in Oracle and Postgres' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Formatting Numbers in Oracle and Postgres' to Technorati" alt="Add 'Formatting Numbers in Oracle and Postgres' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http://www.weheartcode.com/2006/10/09/formatting-numbers-in-oracle/&amp;title=Formatting+Numbers+in+Oracle+and+Postgres" title="Add 'Formatting Numbers in Oracle and Postgres' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Formatting Numbers in Oracle and Postgres' to Google Bookmarks" alt="Add 'Formatting Numbers in Oracle and Postgres' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2006/10/09/formatting-numbers-in-oracle/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
