Skip to content
Advertisement

Converting decimal to Date

I have a column with dates formatted as decimals, for example: 20,210,830.

I want to convert this number to date format as 08/30/2021

I have tried to use convert and the database shoots me an error that convert is not a valid function. Cast seems to work but, only returns a null value every time.

This statement will validate:

SELECT CAST(CAST(CONTCLMPDTE AS VARCHAR(8)) AS DATE) 
FROM CMSFIL.JCTDSC AS COMPLDATE 

This statement works but, just outputs null. For background I am querying from a Db2 database.

My ultimate goal is to use this converted date to grab the difference from the current day.

Such as

DAY(CURRENT_DATE) - DAY(COMPLDATE)

Advertisement

Answer

So after a long couple days and almost pulling my hair out, here is what worked for me.

SELECT date(substr(CONTCLMPDTE,1,4)||'-'||substr(CONTCLMPDTE,5,2)||'-'||substr(CONTCLMPDTE,7,2)) FROM JCTDSC WHERE COMPANYNUMBER={Company Number} AND JOBNUMBER={Job Number} LIMIT 1

This formatted from yyyymmdd to mm/dd/yyyy. It also worked for finding the days between current_date and CONTCLMPDTE using this code.

DAYS(CURRENT_DATE) - DAYS({COntract Compl Date Formatted})

Thank you all for your help!

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement