Search code examples
c#db2ibm-midrangedb2-400

SQL correctly format integers as dates


I am writing a program in C# and a part of it is to grab dates from a DB2 server. The dates are stored as length 4 integer values on the server. The date holds only month and day The problem I have is that they are stored with different accuracy. EDIT: The data type is numeric length 4 with no precision(So an integer length 4) but when the Select statement runs everything is returned as a string. Which is why I was using SubStr().

Example

One date is stored as 1003 representing the date 10/03

Another date is stored as 805 representing 8/05

The SQL code I use for pulling the dates

(SubStr(ML2DDM,0,3) ||'/'|| SubStr(ML2DDM,3,2))as Due__Date

The program returns the dates in the following format

10/03

80/5 <<<< Thats the problem

Is there a way to format the values correctly every time?


Solution

  •  select left(right(repeat('0', 4) || trim(ML2DDM), 4), 2) || '/' ||  
     right(right(repeat('0', 4) || trim(ML2DDM), 4), 2)  as Due__Date