Re: subtract two dates to get the number of days

From: Jean-David Beyer <jeandavid8(at)verizon(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: subtract two dates to get the number of days
Date: 2010-07-14 17:05:04
Message-ID: 4C3DEE40.1040208@verizon.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Thomas Kellerer wrote:
> Jean-David Beyer wrote on 14.07.2010 14:37:
>> My dates are of the form yyyy-mm-dd and such.
> Storing a date as a string is never a good idea.

I started this long ago, when postgreSQL did not really work very well
(1998?). One version of it would not do views, and another had trouble
with primary keys, IIRC. So I first used Informix, until it would not
work any more. It did not really support embedded SQL in C++, only in C,
so that was a pain. But it quit working when Red Hat updated from
release 5 to release 6.

I then moved to IBM's DB2, and that worked very well, but it got too
expensive to keep it when I went from one release of my OS to another
for just my own workstation use. Somewhere around 2004, or a little
before, I decided to give postgreSQL another chance, and it works just fine.

I just looked them up in my data definitions. Dates are _stored_ as type

DATE NOT NULL

so I store them OK. It is just when I want to compute with them that it
gets a bit tricky. Or it did way back when I wrote that stuff in the
late 1990s.
>
>> And I want to do things like adding or subtracting days, months, or years to it or from it.
>> Also the logical comparisons.
> Which is all a piece of cake when you use the proper datatype

Yes, if the data happen to be stored at all. But when a program
generates the dates dynamically and wants to produce queries from them,
it is easier to use the C++ class to generate the dates.
>
>> Years ago, I made a C++ data type that allowed a date datatype where I
>> could add, subtract, and so on.
>> I use it in programs that do not necessarily use a database,

> To be honest: I expect the programming language to support those things.

I would love it. For all I know, the C++ Standard Library supports it
now, but I do not believe it did when I wrote that class.
>
>> but also in programs that do when the computations are the big part of the cpu load,
>> as contrasted to just "gentle" massaging of existing data.
> I would expect doing "date maths" with strings is wasting more CPU than using a native date datatype.

My class pretty much does not do it as strings, but as integers (internally)
>
> Just my €0.02
> Thomas
>
Well, €0.02 is still more than my US$0.02, I believe.

--
.~. Jean-David Beyer Registered Linux User 85642.
/V\ PGP-Key: 9A2FC99A Registered Machine 241939.
/( )\ Shrewsbury, New Jersey http://counter.li.org
^^-^^ 12:45:01 up 6 days, 21:31, 4 users, load average: 4.65, 4.69, 4.71

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Anders Østergaard Jensen 2010-07-14 23:49:25 Re: Question on COUNT performance
Previous Message Lee Hachadoorian 2010-07-14 16:49:14 Re: Question on COUNT performance