Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DB2 SQL Decimal to DATE

I need to convert Decimal to date. I have a decimal date field that contains data like this :

  • 1,132,009.00 --1/13/2009

  • 7,152,004.00 --7/15/2004

  • 11,012,005.00 --11/01/2005

    etc

I would like it to read as xx/xx/xxxx.

Is there anyway to do this with SQL commands or DB2 logic in a select statement?

SELECT  column1  from  table1  ;
like image 862
tdjfdjdj Avatar asked Nov 30 '25 17:11

tdjfdjdj


1 Answers

WITH x(decvalue) AS ( VALUES (DECIMAL(1132009.00)),(DECIMAL(7152004.00)),(DECIMAL(11012005.00)) )

SELECT CAST( 
    LPAD( RTRIM( CHAR( INTEGER( decvalue/1000000 ))), 2, '0' ) || '/' || 
    LPAD( RTRIM( CHAR( MOD( decvalue/10000, 100 ))), 2, '0' )  || '/' ||
    MOD( decvalue, 10000 ) 
AS CHAR(10)) 
AS chardateresult
FROM x
;
like image 156
Fred Sobotka Avatar answered Dec 03 '25 12:12

Fred Sobotka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!