<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Postgresql on David Hamp-Gonsalves</title><link>https://davidhampgonsalves.com/tags/postgresql/</link><description>Recent content in Postgresql on David Hamp-Gonsalves</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Tue, 18 Mar 2014 00:00:00 +0000</lastBuildDate><atom:link href="https://davidhampgonsalves.com/tags/postgresql/index.xml" rel="self" type="application/rss+xml"/><item><title>Postgresql Range Basics</title><link>https://davidhampgonsalves.com/postgresql-range-basics/</link><pubDate>Tue, 18 Mar 2014 00:00:00 +0000</pubDate><guid>https://davidhampgonsalves.com/postgresql-range-basics/</guid><description>&lt;p&gt;In postgres 9.2 a new family of data types were added called &lt;a href="http://www.postgresql.org/docs/9.2/static/rangetypes.html"&gt;Ranges&lt;/a&gt;. This is a quick overview of how to use them.&lt;/p&gt;
&lt;h3 id="range-types"&gt;Range Types&lt;/h3&gt;
&lt;p&gt;There are a handful of built in range types that cover the expected number and date types and you can also define your own if you need something outside of that.&lt;/p&gt;
&lt;h2 id="basic-operations"&gt;Basic Operations&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Defining Range&lt;/strong&gt; - CREATE TABLE ranges (range int4range)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inserting Range&lt;/strong&gt; - INSERT INTO ranges VALUES(&amp;rsquo;[1, 4]')&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Infinate Ranges&lt;/strong&gt; - INSERT INTO ranges VALUES(&amp;rsquo;[4, infinity]')&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Check Range Membership&lt;/strong&gt; - SELECT * FROM ranges where range @&amp;gt; 2&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Get Range Bounds&lt;/strong&gt; - SELECT lower(range), upper(range) FROM ranges&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="defining-ranges-values"&gt;Defining Ranges Values&lt;/h2&gt;
&lt;p&gt;You can define &lt;em&gt;inclusive&lt;/em&gt; ranges using [2,3] and &lt;em&gt;exclusive&lt;/em&gt; ranges using (1,4). You can also &lt;em&gt;mix&lt;/em&gt; them [2,4). You can create &lt;em&gt;infinate ranges&lt;/em&gt; like [4, infinaty] or [-infinity, 4] and you can test for infinity using &lt;em&gt;lower_inf&lt;/em&gt; and &lt;em&gt;upper_inf&lt;/em&gt;. Keep in mind that creating a range like [NULL, 4] will create a unbound lower range. This will act like [infinity, 4] but you wont be able to test for infinity since NULL is still distinct from infinity.&lt;/p&gt;</description></item></channel></rss>