SQL doubt - Date Add

Hi friends,
I problem is i want to add a specified no. of years to a
given date
ie, 12/12/2004 + 5 = 12/12/2009. This has to be done via sql. Any
predefined function there for the same. or v have split the yr and
then add.
Sreejith

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org
getsreejith [ So, 10 Oktober 2004 16:19 ] [ ID #453783 ]

Re: SQL doubt - Date Add

sreejith s wrote:
> Hi friends,
> I problem is i want to add a specified no. of years to a
> given date
> ie, 12/12/2004 + 5 = 12/12/2009. This has to be done via sql. Any
> predefined function there for the same. or v have split the yr and
> then add.
> Sreejith
>

# select '2004-12-12'::date + 5 * '1 year'::interval;
?column?
---------------------
2009-12-12 00:00:00
(1 row)



Regards
Gaetano Mendola
Gaetano Mendola [ So, 10 Oktober 2004 16:36 ] [ ID #453784 ]

Re: SQL doubt - Date Add

sreejith s wrote:
> Hi friends,
> I problem is i want to add a specified no. of years to a
> given date
> ie, 12/12/2004 + 5 = 12/12/2009. This has to be done via sql. Any
> predefined function there for the same. or v have split the yr and
> then add.
> Sreejith
>

# select '2004-12-12'::date + 5 * '1 year'::interval;
?column?
---------------------
2009-12-12 00:00:00
(1 row)



Regards
Gaetano Mendola
Gaetano Mendola [ So, 10 Oktober 2004 16:37 ] [ ID #453785 ]

Re: SQL doubt - Date Add

Gaetano Mendola wrote:
> sreejith s wrote:
>
>> Hi friends,
>> I problem is i want to add a specified no. of years to a
>> given date
>> ie, 12/12/2004 + 5 = 12/12/2009. This has to be done via sql. Any
>> predefined function there for the same. or v have split the yr and
>> then add.
>> Sreejith
>>
>
> # select '2004-12-12'::date + 5 * '1 year'::interval;
> ?column?
> ---------------------
> 2009-12-12 00:00:00
> (1 row)

But be aware of...

richardh=# SELECT '2004-02-28'::date + '1 year'::interval;
?column?
---------------------
2005-02-28 00:00:00
(1 row)

richardh=# SELECT '2004-02-29'::date + '1 year'::interval;
?column?
---------------------
2005-02-28 00:00:00
(1 row)

richardh=# SELECT '2004-02-29'::date + '4 years'::interval;
?column?
---------------------
2008-02-29 00:00:00
(1 row)

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match
dev [ Mo, 11 Oktober 2004 11:21 ] [ ID #453787 ]
Datenbanken » comp.databases.postgresql.sql » SQL doubt - Date Add

Vorheriges Thema: table update using result from join
Nächstes Thema: SELECT with Function