<?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>Florian Helmberger's blog &#187; PostgreSQL</title>
	<atom:link href="http://www.laudatio.com/wordpress/tag/postgresql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.laudatio.com/wordpress</link>
	<description>laudatio.com</description>
	<lastBuildDate>Wed, 15 Jun 2011 17:13:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>A better CONCAT for PostgreSQL</title>
		<link>http://www.laudatio.com/wordpress/2009/04/01/a-better-concat-for-postgresql/</link>
		<comments>http://www.laudatio.com/wordpress/2009/04/01/a-better-concat-for-postgresql/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 19:36:13 +0000</pubDate>
		<dc:creator>fh</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>

		<guid isPermaLink="false">http://www.laudatio.com/wordpress/?p=120</guid>
		<description><![CDATA[Concatination is one thing you need quite often when working with databases and especially when you have to generate some reports. And even as it seems to be a simple problem looking for an easy solution it's a pithole. The natural way would be to do something like this: CREATE TABLE persons &#40; id        SERIAL      [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Concatenation">Concatination </a>is one thing you need quite often when working with databases and especially when you have to generate some reports. And even as it seems to be a simple problem looking for an easy solution it's a pithole. The natural way would be to do something like this:</p>
<pre class="sql"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> persons <span style="color: #66cc66;">&#40;</span>
id        SERIAL      <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>,
firstname TEXT        <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,
surname   TEXT        <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,
title     TEXT
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> persons <span style="color: #66cc66;">&#40;</span>firstname, surname, title<span style="color: #66cc66;">&#41;</span>
  <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Edward'</span>, <span style="color: #ff0000;">'Hyde'</span>, <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> persons <span style="color: #66cc66;">&#40;</span>firstname, surname, title<span style="color: #66cc66;">&#41;</span>
  <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Henry'</span>, <span style="color: #ff0000;">'Jekyll'</span>, <span style="color: #ff0000;">'Dr.'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> * <span style="color: #993333; font-weight: bold;">FROM</span> persons;
&nbsp;
 id | firstname | surname | title
<span style="color: #808080; font-style: italic;">----+-----------+---------+-------</span>
  <span style="color: #cc66cc;">1</span> | Edward    | Hyde    |
  <span style="color: #cc66cc;">2</span> | Henry     | Jekyll  | Dr.
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> rows<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> title || <span style="color: #ff0000;">' '</span> || firstname || <span style="color: #ff0000;">' '</span> || surname <span style="color: #993333; font-weight: bold;">FROM</span> persons;
&nbsp;
?<span style="color: #993333; font-weight: bold;">COLUMN</span>?
<span style="color: #808080; font-style: italic;">------------------</span>
&nbsp;
Dr. Henry Jekyll
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> rows<span style="color: #66cc66;">&#41;</span></pre>
<p>As you see, the row containing the NULL value is omitted. So how to fix that? Of course we might do something like using COALESCE to return at least an empty string instead of a NULL value, but this way we'd get a whitespace we might not want (on the web not that bad because normaly the browser renders multiple whitespace as one).</p>
<p>So, if you research this issue a bit via google you'll find many solutions. From PL/pgSQL CONCAT functions with might handle two, three or four arguments (CONCAT2, CONCAT3, you get the picture) to solutions using custom aggregate functions. But most of them don't care about NULL values and will break.</p>
<p>Let's try this with a simple CONCAT function:</p>
<pre 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> concat<span style="color: #66cc66;">&#40;</span>text, text, text<span style="color: #66cc66;">&#41;</span> RETURNS text <span style="color: #993333; font-weight: bold;">AS</span> $$
<span style="color: #993333; font-weight: bold;">SELECT</span> $<span style="color: #cc66cc;">1</span> || <span style="color: #ff0000;">' '</span> || $<span style="color: #cc66cc;">2</span> || <span style="color: #ff0000;">' '</span> || $<span style="color: #cc66cc;">3</span>;
$$ <span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'sql'</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> CONCAT<span style="color: #66cc66;">&#40;</span>title, firstname, surname<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> persons;
concat
<span style="color: #808080; font-style: italic;">----------------</span>
&nbsp;
Dr. Henry Jekyll
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> rows<span style="color: #66cc66;">&#41;</span></pre>
<p>Still, the same problem with the row containing the NULL value.</p>
<p>While looking into this, I ran over the buildin function <a href="http://www.postgresql.org/docs/8.3/static/functions-array.html#ARRAY-FUNCTIONS-TABLE">ARRAY_TO_STRING</a> which leads the path to a very straight forward solution:</p>
<pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> ARRAY_TO_STRING<span style="color: #66cc66;">&#40;</span>ARRAY<span style="color: #66cc66;">&#91;</span>title, firstname, surname<span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> persons;
&nbsp;
array_to_string
<span style="color: #808080; font-style: italic;">------------------</span>
Edward Hyde
Dr. Henry Jekyll
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span> rows<span style="color: #66cc66;">&#41;</span></pre>
<p>Problem solved!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.laudatio.com/wordpress/2009/04/01/a-better-concat-for-postgresql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PostgreSQL: TO_ASCII &amp; UTF8</title>
		<link>http://www.laudatio.com/wordpress/2008/11/05/postgresql-83-to_ascii-utf8/</link>
		<comments>http://www.laudatio.com/wordpress/2008/11/05/postgresql-83-to_ascii-utf8/#comments</comments>
		<pubDate>Wed, 05 Nov 2008 19:11:29 +0000</pubDate>
		<dc:creator>fh</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[UTF8]]></category>

		<guid isPermaLink="false">http://www.laudatio.com/wordpress/?p=62</guid>
		<description><![CDATA[In the process of fixing our code for an upcoming upgrade of one database version for one of our $-projects I encountered a strange behaviour. Initiual situation: we're moving from PostgreSQL 8.1.3 to the 8.3.5 we're moving from database encoding LATIN1 to UTF8 in our code we're using the TO_ASCII function a few times. And [...]]]></description>
			<content:encoded><![CDATA[<p>In the process of fixing our code for an upcoming upgrade of one database version for one of our $-projects I encountered a strange behaviour. Initiual situation:</p>
<ul>
<li>we're moving from PostgreSQL 8.1.3 to the 8.3.5</li>
<li>we're moving from database encoding LATIN1 to UTF8</li>
<li>in our code we're using the TO_ASCII function a few times.</li>
</ul>
<p>And this combination produces some headaches.</p>
<p>But first a gentle introduction to the TO_ASCII function. It converts any given text into it's ASCII representation. Folks bound to languages with german umlauts or some kind of apostrophes encounter many problems. For example: what should you do if you have to build some kind of index based on the first character of the lastname. Certainly you don't want to have an extra entry with 'Ü', instead you want to but them into the 'U' list. Grand entrance TO_ASCII:</p>
<pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> to_ascii<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Übermeier'</span><span style="color: #66cc66;">&#41;</span>;
Ubermeie</pre>
<p>Works like a charm. Caveat: TO_ASCII only supports LATIN1, LATIN2, LATIN9 and WIN1250 encodings but no UTF8.</p>
<p>Okay, the first guess would be to do something like this:</p>
<pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> to_ascii<span style="color: #66cc66;">&#40;</span>convert_to<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Übermeier'</span>, <span style="color: #ff0000;">'latin1'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
ERROR:  <span style="color: #993333; font-weight: bold;">FUNCTION</span> to_ascii<span style="color: #66cc66;">&#40;</span>bytea<span style="color: #66cc66;">&#41;</span> does <span style="color: #993333; font-weight: bold;">NOT</span> exist</pre>
<p>Bummer. CONVERT_TO returnes <a href="http://www.postgresql.org/docs/8.3/interactive/datatype-binary.html">BYTEA</a>, TO_ASCII only wants TEXT.</p>
<p>There has been some <a href="http://www.archivum.info/pgsql.hackers/2008-08/msg00346.html">discussion going on</a> on the pgsql.hackers mailinglist and frankly I can follow both parties in their point of view. But thanks to <a href="http://okbob.blogspot.com/">Pavel Stehule</a> we have some kind of a hack to sidestep this issue:</p>
<pre class="sql"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> to_ascii<span style="color: #66cc66;">&#40;</span>bytea, name<span style="color: #66cc66;">&#41;</span>
RETURNS text STRICT <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'to_ascii_encname'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> internal;</pre>
<p>This version gladly accepts the BYTEA data returned by CONVERT_TO so we can just use it in this way:</p>
<pre class="sql"><span style="color: #993333; font-weight: bold;">SELECT</span> to_ascii<span style="color: #66cc66;">&#40;</span>convert_to<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Übermeier'</span>, <span style="color: #ff0000;">'latin1'</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">'latin1'</span><span style="color: #66cc66;">&#41;</span>;
Ubermeie</pre>
<p>Problem solved.</p>
<p>Edit: Added fix by eMerzh. Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.laudatio.com/wordpress/2008/11/05/postgresql-83-to_ascii-utf8/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

