dbTalk Databases Forums  

SQL doubt - Date Add

comp.databases.postgresql.sql comp.databases.postgresql.sql


Discuss SQL doubt - Date Add in the comp.databases.postgresql.sql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
sreejith s
 
Posts: n/a

Default SQL doubt - Date Add - 10-10-2004 , 09:19 AM






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


Reply With Quote
  #2  
Old   
Gaetano Mendola
 
Posts: n/a

Default Re: SQL doubt - Date Add - 10-10-2004 , 09:36 AM






sreejith s wrote:
Quote:
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




Reply With Quote
  #3  
Old   
Gaetano Mendola
 
Posts: n/a

Default Re: SQL doubt - Date Add - 10-10-2004 , 09:37 AM



sreejith s wrote:
Quote:
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



Reply With Quote
  #4  
Old   
Richard Huxton
 
Posts: n/a

Default Re: SQL doubt - Date Add - 10-11-2004 , 04:21 AM



Gaetano Mendola wrote:
Quote:
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



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.