IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Formatting date in DB2


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
manugeorge2004

New User


Joined: 17 Jun 2009
Posts: 38
Location: Denver

PostPosted: Thu Dec 03, 2015 3:18 am
Reply with quote

Hi

I have a column in a DB2 table which is of DATE datatype.
The value stored is 2015-12-02.
I need to write a SELECT query which I can run using IKJEFT01 which will format it as '20151202'

I used

SELECT CHAR(YEAR(ONLINE_DATE))||
CHAR(MONTH(ONLINE_DATE))||
CHAR(DAY(ONLINE_DATE)).

But it returned me

2015 12 2

Is there anyway to achieve this. I tried using SUBSTR. But it was throwing some error. Can we use SUBSTR on a DATE column

Any advice is appreciated
Back to top
View user's profile Send private message
manugeorge2004

New User


Joined: 17 Jun 2009
Posts: 38
Location: Denver

PostPosted: Thu Dec 03, 2015 3:23 am
Reply with quote

Please ignore this. Below worked

SELECT SUBSTR(CHAR(ONLINE_DATE),1,4),
SUBSTR(CHAR(ONLINE_DATE),6,2),
SUBSTR(CHAR(ONLINE_DATE),9,2)
Back to top
View user's profile Send private message
Rohit Umarjikar

Global Moderator


Joined: 21 Sep 2010
Posts: 3051
Location: NYC,USA

PostPosted: Thu Dec 03, 2015 3:35 am
Reply with quote

Easy,
Code:
select replace('2015-12-02','-','') from sysibm.sysdummy1
Back to top
View user's profile Send private message
Nic Clouston

Global Moderator


Joined: 10 May 2007
Posts: 2455
Location: Hampshire, UK

PostPosted: Thu Dec 03, 2015 5:32 pm
Reply with quote

Quote:
But it was throwing some error.

No it didn't. Why? Because nothing on the mainframe "throws" an error. You could have said that it errored in the same manner - but it was not an error, just the wrong output.
Back to top
View user's profile Send private message
Marso

REXX Moderator


Joined: 13 Mar 2006
Posts: 1353
Location: Israel

PostPosted: Thu Dec 03, 2015 7:17 pm
Reply with quote

You could also use:
Code:
SELECT VARCHAR_FORMAT(my_date,'YYYYMMDD')
FROM my.table
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Replacing 'YYMMDD' with date, varying... SYNCSORT 3
No new posts Modifying Date Format Using DFSORT DFSORT/ICETOOL 9
No new posts Need to convert date format DFSORT/ICETOOL 20
No new posts Need help to append a date&tsp at... DFSORT/ICETOOL 9
No new posts Fetch data from programs execute (dat... DB2 3
Search our Forums:

Back to Top