If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Working with dates

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-27-07, 14:46
annamaria annamaria is offline
Registered User
 
Join Date: Apr 2007
Posts: 51
Working with dates

How can I add 'x' years (or months or days) to a date? (format YYYYMMDD).

Ex: 2007-04-27 + 3 years

Thank you.

Anna - Verona (Italy)
Reply With Quote
  #2 (permalink)  
Old 04-27-07, 15:03
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
If the date is a DB2 DATE datatype, then you can just add the amount desired (MyDate + 2 years) or (MyDate + 30 DAYS) or (MyDate + 3 months)

Andy
Reply With Quote
  #3 (permalink)  
Old 04-28-07, 14:20
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
And if the date is still a string, just convert it to a date first:
Code:
DATE('2007-04-27') + 3 YEARS
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 04-29-07, 06:28
annamaria annamaria is offline
Registered User
 
Join Date: Apr 2007
Posts: 51
Quote:
Originally Posted by stolze
And if the date is still a string, just convert it to a date first:
Code:
DATE('2007-04-27') + 3 YEARS
Could I also convert a number into a date?

DATE(20070427)

Thank you.

Anna
Reply With Quote
  #5 (permalink)  
Old 04-29-07, 06:37
guyprzytula guyprzytula is offline
Registered User
 
Join Date: Jun 2006
Posts: 471
no input must be character or use char(xxxx) to convert to character and create correct input format with substring or any other function....
__________________
Best Regards, Guy Przytula
DB2 UDB LUW certified V6/7/8
Reply With Quote
  #6 (permalink)  
Old 04-29-07, 13:54
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Numbers can be converted to dates by calling the DATE function. But in that case, the number identifies the number of days to be added to 0001-01-01. So completely different semantics are applied.

You could write your own function like this:
Code:
CREATE FUNCTION NumberToDate(n INTEGER)
   RETURNS DATE
   DETERMINISTIC
   NO EXTERNAL ACTION
   RETURN DATE('0001-01-01') +
      ((n / 10000) - 1) YEARS +
      ((MOD(n / 10000) / 100) - 1) MONTHS +
      ((MOD(n / 100) - 1) DAYS;
Another alternative may be to convert the number to a string and the string to a date. I haven't tried that, though.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 05-02-07, 02:42
gpr_cv@yahoo.com gpr_cv@yahoo.com is offline
Registered User
 
Join Date: May 2007
Posts: 1
Hi Everybody,
Can i get books on db2 procedures and functions?
Which is the good book?
Thanks in advance
Reply With Quote
  #8 (permalink)  
Old 05-04-07, 04:01
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
There is one on SQL/PL written by Paul Yip et al.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On