Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UxADq-0003AE-EV for pgsql-sql@arkaria.postgresql.org; Thu, 11 Jul 2013 06:19:22 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1UxADo-0002qt-VQ for pgsql-sql@arkaria.postgresql.org; Thu, 11 Jul 2013 06:19:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UxADn-0002qm-Rl for pgsql-sql@postgresql.org; Thu, 11 Jul 2013 06:19:19 +0000 Received: from mbx.knossos.net.nz ([202.160.48.10]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1UxADc-0002Ur-Kn for pgsql-sql@postgresql.org; Thu, 11 Jul 2013 06:19:18 +0000 Received: from [10.1.1.3] (60-234-150-59.bitstream.orcon.net.nz [60.234.150.59]) (authenticated bits=0) by mbx.knossos.net.nz (8.14.4/8.14.4) with ESMTP id r6B6IvuJ028958 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Thu, 11 Jul 2013 18:18:57 +1200 Message-ID: <51DE4E51.3050702@archidevsys.co.nz> Date: Thu, 11 Jul 2013 18:18:57 +1200 From: Gavin Flower Organization: ArchiDevSys User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 MIME-Version: 1.0 To: Huan Ruan CC: pgsql-sql@postgresql.org Subject: Re: DateDiff() function References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------090608050801030705080508" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org This is a multi-part message in MIME format. --------------090608050801030705080508 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 11/07/13 17:17, Huan Ruan wrote: > Hi Guys > > We are migrating to Postgres. In the current system, we use datediff() > function to get the difference between two dates, e.g. datediff > (month, cast('2013-01-01' as timestamp), cast('2013-02-02' > as timestamp) returns 1. > > I understand that Postgres has Interval data type so I can achieve the > same with Extract(month from Age(date1, date2)). However, I try to > make it so that the existing SQL can run on both databases without > changes. One possible way is to add a datediff function to Postgres, > but the problem is that month/day/year etc is a keyword not a string > like 'month'. I noticed that Postgres seems to convert Extract(month > from current_timestamp) to date_part('month', current_timestamp), you > can also do Extract('month' from current_timestamp). So it seems > internally, Postgres can do the mapping from month to 'month'. I was > wondering if there is a way for me to do the same for the datediff() > function? Any other ideas? > > Thanks > Huan Purely out of curiosity, could you tell us what database software you are moving from, as well as a rough idea of the size of database, type and volume of database queries? It would also be of interest to know what postgres features in particular were the biggest motivations for change, and any aspects that gave you cause for concern - obviously overall, it must have come across as being better . I strongly suspect that answering these questions will have no direct bearing on how people will answer your query! :-) Cheers, Gavin --------------090608050801030705080508 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
On 11/07/13 17:17, Huan Ruan wrote:
Hi Guys

We are migrating to Postgres. In the current system, we use datediff() function to get the difference between two dates, e.g. datediff (month, cast('2013-01-01' as timestamp), cast('2013-02-02' as timestamp) returns 1.

I understand that Postgres has Interval data type so I can achieve the same with Extract(month from Age(date1, date2)). However, I try to make it so that the existing SQL can run on both databases without changes. One possible way is to add a datediff function to Postgres, but the problem is that month/day/year etc is a keyword not a string like 'month'. I noticed that Postgres seems to convert Extract(month from current_timestamp) to date_part('month', current_timestamp), you can also do Extract('month' from current_timestamp). So it seems internally, Postgres can do the mapping from month to 'month'. I was wondering if there is a way for me to do the same for the datediff() function? Any other ideas?

Thanks
Huan
Purely out of curiosity, could you tell us what database software you are moving from, as well as a rough idea of the size of database, type and volume of database queries?

It would also be of interest to know what postgres features in particular were the biggest motivations for change, and any aspects that gave you cause for concern - obviously overall, it must have come across as being better .

I strongly suspect that answering these questions will have no direct bearing on how people will answer your query! :-)


Cheers,
Gavin
--------------090608050801030705080508--