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 > data format

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-19-10, 03:26
Mathew_paul Mathew_paul is offline
Registered User
 
Join Date: Oct 2007
Posts: 200
data format

hi

table is having a column which has a data type char
but contains date in it
eg:

JT_DT char(10)

'2002-04-18'

can i convert it into 04-18-2002 in any ways like can we write
a stored procedure
rregds
Pual
Reply With Quote
  #2 (permalink)  
Old 03-19-10, 08:39
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,574
Can you elaborate on what you want to do? Also include the DB2 version and OS you are using.

Andy
Reply With Quote
  #3 (permalink)  
Old 03-19-10, 09:59
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
mathew paul, if the value in JT_DT is really a Date, the datatype should be DATE and not CHAR(10). This allows you to use Date functions with less effort and it will take up less storage space (4 bytes compared to 10 bytes). And since DB2 stores Dates (and Time and Timestamps) in a modified pack format, you can display it in any of the standard DB2 formats with:

CHAR(JT_DT, USA)

The formats are ISO/JIS yyyy-mm-dd, USA mm/dd/yyyy or EUR dd.mm.yyyy

However, if this can't be changed or you really want the output in a non-standard DB2 format of dd-mm-yyyy you can use SUBSTR to get what you want:

SUBSTR(JT_DT,6,2) CONCAT '-' CONCAT SUBSTR(JT_DT,9,2) CONCAT '-' CONCAT SUBSTR(JT_DT,1,4)
Reply With Quote
  #4 (permalink)  
Old 03-19-10, 11:11
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,178
Some othet expressions.

Code:
------------------------------ Commands Entered ------------------------------
SELECT jt_dt
     , SUBSTR(jt_dt, 6, 5) || '-' || SUBSTR(jt_dt, 1, 4) expression_1
     , TRANSLATE('ef-gh-abcd', jt_dt, 'abcd-ef-gh')      expression_2
     , REPLACE(CHAR(DATE(jt_dt), USA), '/', '-')         expression_3
     , RIGHT(jt_dt, 5) || '-' || LEFT(jt_dt, 4)          expression_4
  FROM (VALUES CAST('2002-04-18' AS CHAR(10) ) ) AS s(jt_dt)
;
------------------------------------------------------------------------------

JT_DT      EXPRESSION_1 EXPRESSION_2 EXPRESSION_3 EXPRESSION_4         
---------- ------------ ------------ ------------ ---------------------
2002-04-18 04-18-2002   04-18-2002   04-18-2002   04-18-2002           

  1 record(s) selected.
Reply With Quote
  #5 (permalink)  
Old 03-19-10, 11:24
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Quote:
Originally Posted by Mathew_paul View Post
hi

table is having a column which has a data type char
but contains date in it
eg:

JT_DT char(10)

'2002-04-18'

can i convert it into 04-18-2002 in any ways like can we write
a stored procedure
rregds
Pual
It's easy:

Code:
select replace(char(date('2002-04-18'), USA), '/', '-')
  from sysibm.sysdummy1
Lenny
Reply With Quote
  #6 (permalink)  
Old 03-20-10, 03:57
Mathew_paul Mathew_paul is offline
Registered User
 
Join Date: Oct 2007
Posts: 200
thks for reply
i did this way
select substr(del_dt),9,2)||'-'||substr(del_dt),6,2)||'-'||substr(del_dt),1,4) from tabname
it worked
regs
paul
Reply With Quote
  #7 (permalink)  
Old 03-20-10, 04:16
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,178
Quote:
select substr(del_dt),9,2)||'-'||substr(del_dt),6,2)||'-'||substr(del_dt),1,4) from tabname
So, do you want '18-04-2002' from '2002-04-18'?

It's different from original post.
Quote:
eg:

JT_DT char(10)

'2002-04-18'

can i convert it into 04-18-2002 in any ways.....
Reply With Quote
  #8 (permalink)  
Old 03-20-10, 05:58
tonkuma tonkuma is online now
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,178
If you want '18-04-2002' from '2002-04-18',
here are two other ways.

Code:
------------------------------ Commands Entered ------------------------------
SELECT jt_dt
     , TRANSLATE('gh-ef-abcd', jt_dt, 'abcd-ef-gh')      expression_2
     , REPLACE(CHAR(DATE(jt_dt), EUR), '.', '-')         expression_3
  FROM (VALUES CAST('2002-04-18' AS CHAR(10) ) ) AS s(jt_dt)
;
------------------------------------------------------------------------------

JT_DT      EXPRESSION_2 EXPRESSION_3
---------- ------------ ------------
2002-04-18 18-04-2002   18-04-2002  

  1 record(s) selected.
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