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 To String

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-22-09, 09:10
donraja_ht donraja_ht is offline
Registered User
 
Join Date: Sep 2007
Posts: 56
Date To String

I NEED TO CONVERT A DATE type field(column) from a table which is in dd-mm-yyyy format in to a date format dd-mm-yyyy, Also need to know how addition and subtraction is done on date field.

Thanks
Reply With Quote
  #2 (permalink)  
Old 04-22-09, 09:46
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Columns of data type DATE, TIME, and TIMESTAMP do not store the data in the format that you view the data. How you view it depends on a number of factors like your LOCALE.

For date arithmetic see:

DB2 Basics: Fun with Dates and Times

Andy
Reply With Quote
  #3 (permalink)  
Old 04-23-09, 01:06
donraja_ht donraja_ht is offline
Registered User
 
Join Date: Sep 2007
Posts: 56
I got it but say if the date is stored in string mm-dd-yyyy and want to convert it into dd-mm-yyyy form, can this be done in DB2
for example take this Query
select tp.PROJECT_ID id
from expenses as tp
where cast(tp.UPDATED_ON as char) >= cast(CURRENT DATE - 1 as char)
and cast(tp.UPDATED_ON as char) <= cast(CURRENT DATE as char);

How do i check this conditions, and UPDATE_ON is a TIMESTAMP datatype.

Last edited by donraja_ht; 04-23-09 at 01:25.
Reply With Quote
  #4 (permalink)  
Old 04-23-09, 01:32
umayer umayer is offline
Registered User
 
Join Date: Dec 2005
Posts: 273
DATE is stored in an internal format. There is no need to convert it for your query. Simply code:

select tp.PROJECT_ID id
from expenses as tp
where tp.UPDATED_ON >= CURRENT DATE - 1 DAY
and tp.UPDATED_ON as char <= CURRENT DATE;
Reply With Quote
  #5 (permalink)  
Old 04-23-09, 04:26
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
If you have a string that you interpret as a date, i.e. it is a value of type VARCHAR or CHAR but not a DATE, I suggest that you apply the DATE() function to convert the string to a date value and use that. You can do the conversion in a view if you won't/can't change the schema definition.
__________________
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