<?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>Janusz Slota</title>
	<atom:link href="http://janusz.slota.name/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://janusz.slota.name/blog</link>
	<description>My home page</description>
	<lastBuildDate>Thu, 11 Jun 2009 21:18:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>MySQL FROM_UNIXTIME and UNIX_TIMESTAMP functions in PostgreSQL</title>
		<link>http://janusz.slota.name/blog/2009/06/mysql-from_unixtime-and-unix_timestamp-functions-in-postgresql/</link>
		<comments>http://janusz.slota.name/blog/2009/06/mysql-from_unixtime-and-unix_timestamp-functions-in-postgresql/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 20:00:42 +0000</pubDate>
		<dc:creator>Janusz Słota</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://janusz.slota.name/blog/?p=80</guid>
		<description><![CDATA[Personally I prefer PostgreSQL over MySQL DBMS. However there are some things I miss in PostgreSQL. Here is an advice on how to use MySQL FROM_UNIXTIME and UNIX_TIMESTAMP function in PostgreSQL DBMS.

FROM_UNIXTIME()
MySQL query:

mysql&#62; SELECT FROM_UNIXTIME&#40;123456789&#41;;

PostgreSQL equivalent:

postgres=&#62; SELECT to_timestamp&#40;123456789&#41;; -- with time zone
postgres=&#62; SELECT to_timestamp&#40;123456789&#41;::timestamp; -- without time zone

In order to be able to run MySQL [...]]]></description>
			<content:encoded><![CDATA[<p>Personally I prefer PostgreSQL over MySQL DBMS. However there are some things I miss in PostgreSQL. Here is an advice on how to use MySQL <strong>FROM_UNIXTIME</strong> and <strong>UNIX_TIMESTAMP</strong> function in PostgreSQL DBMS.<br />
<span id="more-80"></span></p>
<h2>FROM_UNIXTIME()</h2>
<p>MySQL query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> FROM_UNIXTIME<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">123456789</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>PostgreSQL equivalent:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> to_timestamp<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">123456789</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">-- with time zone</span>
postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> to_timestamp<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">123456789</span><span style="color: #66cc66;">&#41;</span>::timestamp; <span style="color: #808080; font-style: italic;">-- without time zone</span></pre></div></div>

<p>In order to be able to run MySQL style query in PostgreSQL DBMS, you need to create this function:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><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> from_unixtime<span style="color: #66cc66;">&#40;</span>integer<span style="color: #66cc66;">&#41;</span> RETURNS timestamp <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
	 SELECT to_timestamp($1)::timestamp AS result
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'SQL'</span>;</pre></div></div>

<p>then you can run query like this (exactly the same as in MySQL):</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> FROM_UNIXTIME<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">123456789</span><span style="color: #66cc66;">&#41;</span>;
    from_unixtime
<span style="color: #808080; font-style: italic;">---------------------</span>
 <span style="color: #cc66cc;">1973</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">29</span> <span style="color: #cc66cc;">21</span>:<span style="color: #cc66cc;">33</span>:09
<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span> row<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>You can probably find (on the Internet) definitions with keyword <code>abstime</code> (i.e. <code>$1::abstime::timestamp without time zone AS result</code>).<br />
This keyword is obsolete as described <a href="http://www.postgresql.org/docs/current/interactive/datetime-keywords.html#DATETIME-MOD-TABLE">here</a>, so IMHO it&#8217;s better to use the definition you see above.</p>
<h2>UNIX_TIMESTAMP()</h2>
<p>MySQL queries:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
mysql<span style="color: #66cc66;">&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1973-11-29 21:33:09'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>PostgreSQL equivalents:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> EXTRACT<span style="color: #66cc66;">&#40;</span>EPOCH <span style="color: #993333; font-weight: bold;">FROM</span> CURRENT_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> EXTRACT<span style="color: #66cc66;">&#40;</span>EPOCH <span style="color: #993333; font-weight: bold;">FROM</span> TIMESTAMP <span style="color: #ff0000;">'1973-11-29 21:33:09'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Again &#8211; in order to be able to run MySQL style query in PostgreSQL DBMS, you need to create 3 functions:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">-- no params</span>
<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> unix_timestamp<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> RETURNS bigint <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
	SELECT EXTRACT(EPOCH FROM CURRENT_TIMESTAMP(0))::bigint AS result;
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'SQL'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- timestamp without time zone (i.e. 1973-11-29 21:33:09)</span>
<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> unix_timestamp<span style="color: #66cc66;">&#40;</span>timestamp<span style="color: #66cc66;">&#41;</span> RETURNS bigint <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
	SELECT EXTRACT(EPOCH FROM $1)::bigint AS result;
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'SQL'</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">-- timestamp with time zone (i.e. 1973-11-29 21:33:09+01)</span>
<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> unix_timestamp<span style="color: #66cc66;">&#40;</span>timestamp <span style="color: #993333; font-weight: bold;">WITH</span> time zone<span style="color: #66cc66;">&#41;</span> RETURNS bigint <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'
	SELECT EXTRACT(EPOCH FROM $1)::bigint AS result;
'</span> <span style="color: #993333; font-weight: bold;">LANGUAGE</span> <span style="color: #ff0000;">'SQL'</span>;</pre></div></div>

<p>As noted <a href="http://www.postgresql.org/docs/8.0/static/datatype-datetime.html">here</a>, prior to PostgreSQL 7.3, writing just timestamp was equivalent to timestamp with time zone. This was changed for SQL compliance.</p>
<p>Now you can run queries like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1973-11-29 21:33:09'</span><span style="color: #66cc66;">&#41;</span>;
postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1973-11-29 21:33:09'</span> AT TIME ZONE <span style="color: #ff0000;">'GMT'</span><span style="color: #66cc66;">&#41;</span>;
postgres<span style="color: #66cc66;">=&gt;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> UNIX_TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1973-11-29 21:33:09+01'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Tested on: PostgreSQL 8.1.11 (debian), PostgreSQL 8.2.7 (gentoo) and MySQL 5.0.70 (gentoo)</p>
]]></content:encoded>
			<wfw:commentRss>http://janusz.slota.name/blog/2009/06/mysql-from_unixtime-and-unix_timestamp-functions-in-postgresql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What&#8217;s the average length of an email address?</title>
		<link>http://janusz.slota.name/blog/2009/05/email-length/</link>
		<comments>http://janusz.slota.name/blog/2009/05/email-length/#comments</comments>
		<pubDate>Tue, 05 May 2009 16:52:18 +0000</pubDate>
		<dc:creator>Janusz Słota</dc:creator>
				<category><![CDATA[Howto]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[postgres]]></category>

		<guid isPermaLink="false">http://janusz.slota.name/blog/?p=1</guid>
		<description><![CDATA[This question always comes up, when you have to design new table in database to store email addresses. Is VARCHAR(30) long enough? Or maybe you should use VARCHAR(255) or maybe TEXT as I&#8217;ve seen in some projects?
My live database, which I&#8217;ve used for this test contains 92298 valid email addresses, verified with the Pear Validate [...]]]></description>
			<content:encoded><![CDATA[<p>This question always comes up, when you have to design new table in database to store email addresses. Is <strong>VARCHAR(30)</strong> long enough? Or maybe you should use <strong>VARCHAR(255)</strong> or maybe <strong>TEXT</strong> as I&#8217;ve seen in some projects?</p>
<p>My live database, which I&#8217;ve used for this test contains <strong>92298</strong> valid email addresses, verified with the <a href="http://pear.php.net/package/Validate" target="_blank">Pear Validate</a> class with domain checking.<br />
<span id="more-1"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>Validate<span style="color: #339933;">::</span><span style="color: #004000;">email</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// add to database</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// return error</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It means, if the email address is in this database &#8211; it is real and correct, otherwise the field is empty. So to make sure not to include empty fields, I&#8217;ve issued following query:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> count<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> length<span style="color: #66cc66;">&#40;</span>email<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> size <span style="color: #993333; font-weight: bold;">FROM</span> customer <span style="color: #993333; font-weight: bold;">WHERE</span> email <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%@%'</span> <span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> size <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> size;</pre></div></div>

<p>This query returns set of values, which I&#8217;ve used to create this chart (with <a title="OpenOffice.org" href="http://openoffice.org" target="_blank">OpenOffice.org</a> Calc).</p>
<p style="text-align: center;"><img class="size-full wp-image-12 aligncenter" title="Average emial address length" src="http://janusz.slota.name/blog/wp-content/uploads/2009/05/email-length-stats_html_54813cd4.jpg" alt="email-length-stats_html_54813cd4" width="815" height="241" /></p>
<p>As you can see majority of email addresses are around 13 to 34 characters long. To store email addresses I&#8217;ve used <strong>VARCHAR (255)</strong> but as you can see <strong>VARCHAR(50)</strong> is enough.</p>
<p>The answer: <strong>VARCHAR(50)</strong> should do.</p>
]]></content:encoded>
			<wfw:commentRss>http://janusz.slota.name/blog/2009/05/email-length/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
