Thread: RE: [SQL] datediff function

RE: [SQL] datediff function

From
"Pham, Thinh"
Date:
Thank you for answering all my stupid questions. I did read the User manual
as well as Programmer and Admin, and what i was able to get out was not
much. I guess i wasn't used to the way that postgres work coming from a
microsoft background. But wouldn't you agree that the manuals was a little
brief and criptic at times? My question is how do i go about writing some
more documentations and examples and submit it for inclusion into the
manual. It just so that the next person w/ the same background as i would be
able to understand those functions quickly and be able to use the example
and apply it to his program w/o asking all those same basic questions which
i'm sure you guys are tired of answering.

Also, is it the same thing for submitting documentation as well as function
into postgres? What i really need was the total amount of time between 2
separate point of times. If i ask for 'day' then it return day, 'minute'
then it return minutes. For example:

Timein                Timeout
Tue Aug 17 15:00:00 1999 CDT    Tue Aug 17 16:00:00 1999 CDT

select datediff(day, timein, timeout) as totaltime from schedule

Would give me a _number_ 0 since it's the same day, and if i used minute as
below:


select datediff(minute, timein, timeout) as totaltime from schedule

It would give me the number 60, that's it. I don't want any qualifier behind
the number since it blew up the stupid microsoft ADO driver like you
wouldn't believe.

Thank you,
Thinh


> -----Original Message-----
> From: Herouth Maoz [mailto:herouth@oumail.openu.ac.il]
> Sent: Tuesday, August 17, 1999 8:29 AM
> To: Pham, Thinh; 'pgsql-sql@postgreSQL.org'
> Subject: RE: [SQL] datediff function
> 
> 
> At 16:18 +0300 on 17/08/1999, Pham, Thinh wrote:
> 
> 
> > What happen if i just want to compare using minute only or 
> hour only instead
> > of day? Is there a function to do that or is postgres only 
> work in day?
> 
> No problem, just write 'now'::datetime - '6 hours'::timespan. 
> Or some such.
> Please read about the datetime and timespan types in the user guide.
> 
> Herouth
> 
> --
> Herouth Maoz, Internet developer.
> Open University of Israel - Telem project
> http://telem.openu.ac.il/~herutma
> 
> 
> 


RE: [SQL] datediff function

From
Herouth Maoz
Date:
At 17:37 +0300 on 17/08/1999, Pham, Thinh wrote:


> select datediff(minute, timein, timeout) as totaltime from schedule
>
> It would give me the number 60, that's it. I don't want any qualifier behind
> the number since it blew up the stupid microsoft ADO driver like you
> wouldn't believe.

If you don't want to write 'now'::datetime you can always write
datetime('now'). Same goes for '1 week'::timespan and timespan( '1 week' ).
I don't think this will blow up your Microsoft product, but then again,
anything can blow up a Microsoft product, being a Microsoft Product
included...

To make things clear, here is what Postgres can and cannot do:

It can give you the interval between two dates. The returned value is an
integer representing the number of days between them.

It can give you the interval between two datetimes. The returned value is a
timespan, expressing days, hours, minutes, etc. as needed.

Another method to get the same thing is using age( datetime1, datetime2 ).
This returns a timespan, but expressed in years, months, days, hours and
minutes. There is a subtle difference here, because a year is not always
365 days, and a month is 28-31 days, depending...

You can also truncate datetimes, dates, and other date related types, to
the part of your choice. Truncate it to the minute, and it drops the
seconds, and gives it back to you with 00 in the seconds. Truncate it to
days and it gives it back to you at 00:00:00. This is done with
date_trunc().

Another useful operation which can be done is taking one part of the
datetime (or related type). For example, the minutes, the seconds, the day,
the day of week, or the seconds since the epoch.

Now, I'm not sure these functions do exactly what you wanted. It depends on
what you expect from datediff(minute, timein, itmeout) when they are not on
the same day. For 13-oct-1999 14:00:00 and 14-oct-1999 14:00:05, do you
expect 5 or 24*60 + 5?

If only 5, then you can do it with

SELECT date_part( 'minute', datetime1 - datetime2 )

If not, you will have to do the 24*60 calculation in full.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma