简体   繁体   中英

Convert Decimal to Date in DB2

I have a column in my table having date value as Decimal like 20180715 for 15-07-2018. I want to convert it to MMDDYYYY format. How to do it? Means, I have a decimal value 20180715 and want to convert it like 07152018.

try somthing like this:

select 
VARCHAR_FORMAT( TIMESTAMP_FORMAT(cast(yourcolumn as varchar(8)), 'YYYYMMDD') , 'MMDDYYYY') 
from yourtable

but you you want a really date do it:

select 
DATE( TIMESTAMP_FORMAT(cast(yourcolumn as varchar(8)), 'YYYYMMDD')) 
from yourtable

Try this query for the date

select 
date(timestamp_format(char(yourcolumn+19000000), 'YYYYMMDD')) 
from yourtable

To get the time

select 
cast( substr( right( '00' || yourcolumn, 6) ,1,2) || ':' || substr( right( '00' || yourcolumn, 6) ,3,2) || ':' || substr( right( '00' || yourcolumn, 6) ,5,2) as time) 
from yourtable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM