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 > Converting DATE to CHAR

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-19-09, 21:00
Bambuk Bambuk is offline
Registered User
 
Join Date: Sep 2008
Posts: 11
Converting DATE to CHAR

There is date column on the DB2 table (DD/MM/YYYY). I need to create a view so that the date is displayed as a CHAR (format DDMMYYYY).
Tried CHAR_TO and Variable_format functions, but they do not work.
How should I code the select?

Thanks.
Reply With Quote
  #2 (permalink)  
Old 11-19-09, 22:19
DB2Plus DB2Plus is offline
Registered User
 
Join Date: Jul 2009
Posts: 150
Thumbs up easy

Code:
select replace(varchar(col1), '/', '') from your_table
Kara
Reply With Quote
  #3 (permalink)  
Old 11-20-09, 00:47
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
The DD/MM/YYYY format is dependent on the client you use to access the database. It will not display that way if the client has US territory code, or with a client that uses the ISO format YYYY-MM-DD.

Regardless of the territory code, all dates are stored internally in DB2 as YYYYMMDD in 4 bytes (two numbers per byte since each byte is capable of storing 2 hexidecimal digits 0-F).
__________________
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 11-20-09, 03:34
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
DATE to CHAR (format DDMMYYYY).

Code:
------------------------------ Commands Entered ------------------------------
SELECT date_
     , REPLACE(CHAR(date_, EUR), '.', '')            AS "DDMMYYYY(replace.char)"
     , TRANSLATE('ghefabcd', HEX(date_), 'abcdefgh') AS "DDMMYYYY(translate.hex)"
     , SUBSTR(TO_CHAR(date_, 'DDMMYYYY'), 1, 8)      AS "DDMMYYYY(to_char)"
  FROM (VALUES current_date) q(date_);
------------------------------------------------------------------------------

DATE_      DDMMYYYY(replace.char) DDMMYYYY(translate.hex) DDMMYYYY(to_char)
---------- ---------------------- ----------------------- -----------------
2009-11-20 20112009               20112009                20112009         

  1 record(s) selected.

Last edited by tonkuma; 11-20-09 at 03:51.
Reply With Quote
  #5 (permalink)  
Old 11-20-09, 03:57
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
TO_CHAR() returns VARCHAR(254).

Code:
------------------------------ Commands Entered ------------------------------
DESCRIBE 
SELECT date_
     , REPLACE(CHAR(date_, EUR), '.', '')            AS "DDMMYYYY(replace.char)"
     , TRANSLATE('ghefabcd', HEX(date_), 'abcdefgh') AS "DDMMYYYY(translate.hex)"
     , SUBSTR(TO_CHAR(date_, 'DDMMYYYY'), 1, 8)      AS "DDMMYYYY(substr.to_char)"
     , TO_CHAR(date_, 'DDMMYYYY')                    AS "DDMMYYYY(to_char)"
  FROM (VALUES current_date) q(date_);
------------------------------------------------------------------------------

 Column Information

 Number of columns: 5

 SQL type              Type length  Column name                     Name length
 --------------------  -----------  ------------------------------  -----------
 384   DATE                     10  DATE_                                     5
 448   VARCHAR                  10  DDMMYYYY(replace.char)                   22
 448   VARCHAR                   8  DDMMYYYY(translate.hex)                  23
 452   CHARACTER                 8  DDMMYYYY(substr.to_char)                 24
 448   VARCHAR                 254  DDMMYYYY(to_char)                        17
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