Tuesday, November 10, 2015

Working with Date Column in SQL using IBM DB2

I today encountered a situation where I was supposed to convert a Decimal field to a Date column and then check if the from and To date is within 45 Days.

To convert a Decimal field to a Date, you can you below command :


TO_DATE(CHAR(REQUESTED_DATE),'mmddyyyy') 
BETWEEN 
TO_DATE(CHAR(FROM_DATE),'mmddyyyy') 
AND       
TO_DATE(CHAR(TO_DATE),'mmddyyyy')             


And to check if the difference between the From and To Date is 45 :

For this, you can use Days function :

 IF Days(TO_DATE(CH1AR(TO_DATE),'mmddyyyy')) -         
 Days(TO_DATE(CHAR(FROM_DATE),'mmddyyyy')) > 45  THEN 
{  Condition --------------- }



To convert from a Date field to Decimal we need to use below command :

Dec(Replace(Char(Date('REQUESTED DATE'),ISO),'-','')) 


Hope this solves any blockers faced while coding :)

No comments:

Post a Comment