Coverting INTEGER value to Date format

Database

Coverting INTEGER value to Date format

I have a scenario where my date value is there in INTEGER and the format is 40205 .

Here 4 means 2004 and 02 is Month and 05 is Day

i wanna convert this in to Date . When try to convert it into Date it's showing an wrong year

sel cast( 40205 as date format 'DD/MM/YYYY')

Result : 05/02/1904 here instead of 2004 it is showing it as 1904 .

would you please help me in this Issue

6 REPLIES

Re: Coverting INTEGER value to Date format

any one please help me its urgent
mjj
Teradata Employee

Re: Coverting INTEGER value to Date format

sel cast((cast('200' || '40205' as char(8))) as date format 'yyyymmdd')

can work .........

Re: Coverting INTEGER value to Date format

Thank you mjj ,But it is wrong for this format 100221

Re: Coverting INTEGER value to Date format

Two ways:

The obvious one:

SELECT CAST(CAST(20000000+your_date AS CHAR(8)) AS DATE FORMAT 'YYYYMMDD');

F.Ex.:

SELECT CAST(CAST(20000000+100221 AS CHAR(8)) AS DATE FORMAT 'YYYYMMDD');

*** Query completed. One row found. One column returned.
*** Total elapsed time was 1 second.

(20000000+100221)
-----------------
20100221

The cool one:

SELECT CAST(1000000+your_date AS DATE);

F.Ex.:

SELECT CAST(1000000+100221 AS DATE);

*** Query completed. One row found. One column returned.
*** Total elapsed time was 1 second.

(1000000+100221)
----------------
2010-02-21

HTH.

Cheers.

Carlos.

Re: Coverting INTEGER value to Date format

Thank you for your reply carlosAL
Teradata Employee

Re: Coverting INTEGER value to Date format

@CarlosAL .... indeed a cool solution!! :)