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 > Convert Integer to a Date Value

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-05-11, 05:58
mr-sansibar mr-sansibar is offline
Registered User
 
Join Date: Jun 2009
Posts: 9
Convert Integer to a Date Value

Hi,
how i can a integer value 201108 convert into date-value in this format YYYYMMDD.

for example
201108->20110801
201109->20110901
201110->20111001
201111->20111101

and so on
i try onthis way date((char(Year_Month)||'01')
but its not working.

Thank you
Reply With Quote
  #2 (permalink)  
Old 08-05-11, 06:42
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
YYYYMMDD is not an usual string representation of a date datatype.

You can covert the integer values to VARCHAR or INTEGER datatype with format 'YYYYMMDD'.

Example:
Code:
------------------------------ Commands Entered ------------------------------
SELECT source_int
     , source_int * 100 + 1 AS int_yyyymmdd
     , RIGHT( DIGITS(source_int * 100 + 1) , 8 ) AS varchar_ymd_1
     , RIGHT( DIGITS(source_int) , 6 ) || '01'   AS varchar_ymd_2
     , VARCHAR(source_int) || '01'               AS varchar_ymd_3
 FROM  (VALUES
               201108 -->20110801
             , 201109 -->20110901
             , 201110 -->20111001
             , 201111 -->20111101
       ) s(source_int)
;
------------------------------------------------------------------------------

SOURCE_INT  INT_YYYYMMDD VARCHAR_YMD_1 VARCHAR_YMD_2 VARCHAR_YMD_3
----------- ------------ ------------- ------------- -------------
     201108     20110801 20110801      20110801      20110801     
     201109     20110901 20110901      20110901      20110901     
     201110     20111001 20111001      20111001      20111001     
     201111     20111101 20111101      20111101      20111101     

  4 record(s) selected.

Last edited by tonkuma; 08-05-11 at 06:51. Reason: Add varchar_ymd_3 to Example.
Reply With Quote
  #3 (permalink)  
Old 08-05-11, 07:06
manojkumar2003 manojkumar2003 is offline
Registered User
 
Join Date: Jul 2011
Posts: 5
Smile Convert Integer to a Date Value

Hi,
Its very easy use the below mention query.

SQL> SELECT TO_NUMBER(TO_CHAR(TO_DATE(TO_CHAR(201108),'YYYYMM' ),'YYYYMMDD')) FROM DUAL;

TO_NUMBER(TO_CHAR(TO_DATE(TO_CHAR(201108),'YYYYMM' ),'YYYYMMDD'))
----------------------------------------------------------------
20110801
Reply With Quote
  #4 (permalink)  
Old 08-05-11, 07:24
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
if I am not wrong this is for Ora... because of dual (sysibm.sysdummy1 in db2) and functions
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #5 (permalink)  
Old 08-05-11, 07:51
manojkumar2003 manojkumar2003 is offline
Registered User
 
Join Date: Jul 2011
Posts: 5
Smile Convert Integer to a Date Value

yes this is oracle query
Reply With Quote
  #6 (permalink)  
Old 08-05-11, 08:12
mr-sansibar mr-sansibar is offline
Registered User
 
Join Date: Jun 2009
Posts: 9
thank you im using Db2 and looks so for this question:

integer(month)*100+1 as month
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