casting for dates

Started by Vince Vielhaberover 24 years ago6 messages
#1Vince Vielhaber
vev@michvhf.com

I'm trying to use an integer from a table to add/subtract time in months.
IOW:

create table foo(nummonths int);

select now() - nummonths months;

So far nothing I've tried will work - short of a function. Is there a
way to do this?

Vince.
--
==========================================================================
Vince Vielhaber -- KA8CSH email: vev@michvhf.com http://www.pop4.net
56K Nationwide Dialup from $16.00/mo at Pop4 Networking
Online Campground Directory http://www.camping-usa.com
Online Giftshop Superstore http://www.cloudninegifts.com
==========================================================================

#2Mitch Vincent
mvincent@cablespeed.com
In reply to: Vince Vielhaber (#1)
Re: casting for dates

Will

SELECT now() - 'nummonths months'::interval ;

work?

----- Original Message -----
From: "Vince Vielhaber" <vev@michvhf.com>
To: <pgsql-hackers@postgresql.org>
Sent: Wednesday, September 26, 2001 4:30 PM
Subject: [HACKERS] casting for dates

Show quoted text

I'm trying to use an integer from a table to add/subtract time in months.
IOW:

create table foo(nummonths int);

select now() - nummonths months;

So far nothing I've tried will work - short of a function. Is there a
way to do this?

Vince.
--
==========================================================================
Vince Vielhaber -- KA8CSH email: vev@michvhf.com http://www.pop4.net
56K Nationwide Dialup from $16.00/mo at Pop4 Networking
Online Campground Directory http://www.camping-usa.com
Online Giftshop Superstore http://www.cloudninegifts.com
==========================================================================

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

#3Andrew McMillan
andrew@catalyst.net.nz
In reply to: Vince Vielhaber (#1)
Re: casting for dates

On Thu, 2001-09-27 at 08:30, Vince Vielhaber wrote:

I'm trying to use an integer from a table to add/subtract time in months.
IOW:

create table foo(nummonths int);

select now() - nummonths months;

newsroom=# select now() - interval( text(3) || ' months');
?column?
------------------------
2001-06-27 08:56:27+12
(1 row)

Crude, but hey: it works :-)

Cheers,
SAndrew.
--
--------------------------------------------------------------------
Andrew @ Catalyst .Net.NZ Ltd, PO Box 11-053, Manners St, Wellington
WEB: http://catalyst.net.nz/ PHYS: Level 2, 150-154 Willis St
DDI: +64(4)916-7217 MOB: +64(21)635-694 OFFICE: +64(4)499-2267

#4Ryan Mahoney
ryan@paymentalliance.net
In reply to: Vince Vielhaber (#1)
Re: casting for dates

Haven't tried yet, but perhaps casting nummonths to an interval datatype
would do the trick.

-r

At 04:30 PM 9/26/01 -0400, Vince Vielhaber wrote:

Show quoted text

I'm trying to use an integer from a table to add/subtract time in months.
IOW:

create table foo(nummonths int);

select now() - nummonths months;

So far nothing I've tried will work - short of a function. Is there a
way to do this?

Vince.
--
==========================================================================
Vince Vielhaber -- KA8CSH email: vev@michvhf.com http://www.pop4.net
56K Nationwide Dialup from $16.00/mo at Pop4 Networking
Online Campground Directory http://www.camping-usa.com
Online Giftshop Superstore http://www.cloudninegifts.com
==========================================================================

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.251 / Virus Database: 124 - Release Date: 4/26/01

#5Vince Vielhaber
vev@michvhf.com
In reply to: Andrew McMillan (#3)
Re: casting for dates

On 27 Sep 2001, Andrew McMillan wrote:

On Thu, 2001-09-27 at 08:30, Vince Vielhaber wrote:

I'm trying to use an integer from a table to add/subtract time in months.
IOW:

create table foo(nummonths int);

select now() - nummonths months;

newsroom=# select now() - interval( text(3) || ' months');
?column?
------------------------
2001-06-27 08:56:27+12
(1 row)

Crude, but hey: it works :-)

It certainly does! Thanks!

Vince.
--
==========================================================================
Vince Vielhaber -- KA8CSH email: vev@michvhf.com http://www.pop4.net
56K Nationwide Dialup from $16.00/mo at Pop4 Networking
Online Campground Directory http://www.camping-usa.com
Online Giftshop Superstore http://www.cloudninegifts.com
==========================================================================

#6Vince Vielhaber
vev@michvhf.com
In reply to: Mitch Vincent (#2)
Re: casting for dates

On Wed, 26 Sep 2001, Mitch Vincent wrote:

Will

SELECT now() - 'nummonths months'::interval ;

work?

Unfortunately no.

Vince.
--
==========================================================================
Vince Vielhaber -- KA8CSH email: vev@michvhf.com http://www.pop4.net
56K Nationwide Dialup from $16.00/mo at Pop4 Networking
Online Campground Directory http://www.camping-usa.com
Online Giftshop Superstore http://www.cloudninegifts.com
==========================================================================