Link to home
Start Free TrialLog in
Avatar of JBart_17
JBart_17Flag for United States of America

asked on

AS400 SQL DATEDIFF()

I have to date fields stored as integers example 1080101 is equal to 01/01/2008 I need to see the difference in days
I am doing this in SQL inside iSeries navigator.
I already do some string manipulation to get the date into 'CCYY-MM-DD' format
I know your suppose to be able to do something like this
 ( Date('2008-03-03') - '2008-01-03') as difference
But I am getting weird results for the above dates. Instead of -33 ,  I get a result of  -200

What am I doing wrong?

select
   INV.FEAENB as FinancialDiv
   , INV.FEKNNB as CustomerNum 
   , INV.FEGGNB as InvoiceNum 
   , (substr(INV.FEGHNB,4,2) ||'/'|| substr(INV.FEGHNB,6,2) ||'/'|| substr(INV.FEGHNB,2,2) ) as InvoiceDate
   , (substr(INV.FEHZNB,4,2) ||'/'|| substr(INV.FEHZNB,6,2) ||'/'|| substr(INV.FEHZNB,2,2) ) as DueDate
   , (substr(SET.A4C6DT,4,2) ||'/'|| substr(SET.A4C6DT,6,2) ||'/'|| substr(SET.A4C6DT,2,2) ) as DatePaid  
   ,(date(substr((SET.A4C6DT + 19000000),1,4)||'-'|| substr((SET.A4C6DT + 19000000),5,2)||'-'|| substr((SET.A4C6DT + 19000000),7,2))  -    substr((INV.FEHZNB + 19000000),1,4)||'-'|| substr((INV.FEHZNB + 19000000),5,2)||'-'|| substr((INV.FEHZNB + 19000000),7,2)
) as diff
from
   AMFLIBA.MBF9REP as INV
join
   AMFLIBA.YAA4REP as SET
on
   cast (INV.FEAENB as char(2))  = substr(SET.A4F7CD,1,2)
   and cast(INV.FEGGNB as CHAR(14) ) =  SET.A4PLNA   
   and substr(cast(INV.FEABBJ as CHAR(8) ),1,6) = substr(A4ADCD,1,6)
   and SET.A4EGST = '1'
left outer join
   AMFLIBA.YAB5REP as ALOC 
on
  SET.A4AKNB = ALOC.B5AUNB
  and SET.A4CANB = ALOC.B5GXNB
where
   FEGHNB >=1080101
   and FEGHNB<=10861231
   and FEKNNB = 20011202

Open in new window

SOLUTION
Avatar of momi_sabag
momi_sabag
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JBart_17

ASKER

I found a solution:
(days('2008-01-03') - days('2008-03-03')) as diff
gave the anticipated results
with that being said , understanding the resuls i was reciving is a valuble thing to know so thanks for the info.