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 > Date Difference

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-19-09, 16:15
JimmyTwo JimmyTwo is offline
Registered User
 
Join Date: Sep 2009
Posts: 3
Date Difference

Hi,

I'm using the following to calculate the number of days between 2 dates:
Code:
DAYS(curdate()) - DAYS ("2009-07-01")
The date format of my hardcoded string is yyyy-mm-dd. The return format of curdate() is also currently yyyy-mm-dd because this is the default on the DB2 server. But what if that default is changed and the returned format of curdate() is changed to something else like, mm/dd/yyyy? Will my function still work? How does DB2 know the format to use when it's not even specified? When calculating the difference of days in my above code, can (and how does) DB2 distinguish between yyyy-mm-dd and yyyy-dd-mm?
Reply With Quote
  #2 (permalink)  
Old 09-19-09, 17:04
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
JimmyTwo, the format of the date is immaterial to DB2. It is just a display format. DB2 stores the actual date in internal format using 4 bytes. So 2009-09-07 (or 09/07/2009) is stored as:
Code:
2000
0997
You can prove this with days(date('2009-09-07')) - days(date('2008-01-20')).

Change the formats of the dates to any valid DB2 format and you will get the same result.

PS I don't know that curdate is a DB2 function. I have seen it in the SQL Server / Access world but never in DB2. What Edition / Version of DB2 are you using it in? Or are you using access as a front end to DB2?
Reply With Quote
  #3 (permalink)  
Old 09-19-09, 17:32
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Quote:
Originally Posted by JimmyTwo
When calculating the difference of days in my above code, can (and how does) DB2 distinguish between yyyy-mm-dd and yyyy-dd-mm?
DB2 will accept the ISO format YYYY-MM-DD or the format defined by territory code (USA, EUR, JIS) of the client. The format of YYYY-DD-MM is not a valid format for any of the above territory codes, so DB2 can figure it which one you are using. It is always best to use ISO format, so you don't have to worry about what territory code you happen to be using.

As mentioned by Stealth_DBA the date is always stored internally within DB2 as YYYYMMDD in a format similar to mainframe packed decimal without the signed last half byte, so only 4 bytes are needed.

For the current data, use current_date (although 'current date' also works).
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #4 (permalink)  
Old 09-19-09, 19:24
JimmyTwo JimmyTwo is offline
Registered User
 
Join Date: Sep 2009
Posts: 3
Thanks for the help!
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