<?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; postgres</title>
	<atom:link href="http://www.weheartcode.com/category/postgres/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>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-2">
<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>Postgres Array Joins</title>
		<link>http://www.weheartcode.com/2007/07/25/postgres-array-joins/</link>
		<comments>http://www.weheartcode.com/2007/07/25/postgres-array-joins/#comments</comments>
		<pubDate>Wed, 25 Jul 2007 13:27:03 +0000</pubDate>
		<dc:creator>jason</dc:creator>
				<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://www.weheartcode.com/2007/07/25/postgres-array-joins/</guid>
		<description><![CDATA[Every once in a while you'll come across the need to create a temp table to perform a complicated select query.  Not being a big fan of creating these temp tables, I came up with a better solution.  Leveraging Postgresql's array structure, I wrote a function that converts a varchar[] to a record [...]]]></description>
			<content:encoded><![CDATA[<p>Every once in a while you'll come across the need to create a temp table to perform a complicated select query.  Not being a big fan of creating these temp tables, I came up with a better solution.  Leveraging Postgresql's array structure, I wrote a function that converts a varchar[] to a record set.</p>
<div class="syntax_hilite">
<div id="sql-5">
<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> array_to_rows<span style="color:#006600; font-weight:bold;">&#40;</span> character varying<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
RETURNS SETOF character varying <span style="color: #993333; font-weight: bold;">AS</span><br />
$BODY$<br />
DECLARE<br />
in_array alias <span style="color: #993333; font-weight: bold;">FOR</span> $<span style="color: #cc66cc;">1</span>;</p>
<p>out_varchar varchar;<br />
BEGIN<br />
<span style="color: #993333; font-weight: bold;">FOR</span> i <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #cc66cc;">1</span>..array_upper<span style="color:#006600; font-weight:bold;">&#40;</span>in_array,<span style="color: #cc66cc;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> loop<br />
<span style="color: #993333; font-weight: bold;">RETURN</span> next in_array<span style="color:#006600; font-weight:bold;">&#91;</span>i<span style="color:#006600; font-weight:bold;">&#93;</span>;<br />
end loop;</p>
<p><span style="color: #993333; font-weight: bold;">RETURN</span>;<br />
END<br />
$BODY$<br />
<span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'plpgsql'</span> STABLE;</div>
</div>
</div>
<p></p>
<p>This allows me to pass in an unknown amount of data and perform a LEFT OUTER JOIN on it without losing my orignal data.</p>
<div class="syntax_hilite">
<div id="sql-6">
<div class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span></p>
<p>a.name, b.name, b.other_info</p>
<p><span style="color: #993333; font-weight: bold;">FROM</span></p>
<p><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> array_to_rows <span style="color: #993333; font-weight: bold;">AS</span> name <span style="color: #993333; font-weight: bold;">FROM</span> array_to_rows<span style="color:#006600; font-weight:bold;">&#40;</span>ARRAY<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color: #ff0000;">'JOE'</span>, <span style="color: #ff0000;">'SCOTT'</span>, <span style="color: #ff0000;">'BOB'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> a</p>
<p><span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> names b <span style="color: #993333; font-weight: bold;">ON</span> a.name ilike <span style="color: #ff0000;">'%'</span> || b.name || <span style="color: #ff0000;">'%'</span></div>
</div>
</div>
<p></p>
<p>Hope you find this useful!</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/07/25/postgres-array-joins/&amp;title=Postgres+Array+Joins" title="Add 'Postgres Array Joins' to Del.icio.us"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/delicious.png" title="Add 'Postgres Array Joins' to Del.icio.us" alt="Add 'Postgres Array Joins' 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/07/25/postgres-array-joins/&amp;title=Postgres+Array+Joins" title="Add 'Postgres Array Joins' to digg"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/digg.png" title="Add 'Postgres Array Joins' to digg" alt="Add 'Postgres Array Joins' 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+Array+Joins&amp;u=http://www.weheartcode.com/2007/07/25/postgres-array-joins/" title="Add 'Postgres Array Joins' to FURL"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/furl.png" title="Add 'Postgres Array Joins' to FURL" alt="Add 'Postgres Array Joins' 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/07/25/postgres-array-joins/&amp;title=Postgres+Array+Joins" title="Add 'Postgres Array Joins' to reddit"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/reddit.png" title="Add 'Postgres Array Joins' to reddit" alt="Add 'Postgres Array Joins' 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/07/25/postgres-array-joins/" title="Add 'Postgres Array Joins' to Technorati"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/technorati.png" title="Add 'Postgres Array Joins' to Technorati" alt="Add 'Postgres Array Joins' 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/07/25/postgres-array-joins/&amp;title=Postgres+Array+Joins" title="Add 'Postgres Array Joins' to Google Bookmarks"><img src="http://www.weheartcode.com/wp-content/plugins/social_bookmarks/google.png" title="Add 'Postgres Array Joins' to Google Bookmarks" alt="Add 'Postgres Array Joins' to Google Bookmarks" /></a></div>
<!-- Social Bookmarks END -->]]></content:encoded>
			<wfw:commentRss>http://www.weheartcode.com/2007/07/25/postgres-array-joins/feed/</wfw:commentRss>
		<slash:comments>1</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-9">
<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-10">
<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>
