IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Pull data using date difference between 2 date fields


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Krishnadeva Reddy

New User


Joined: 14 Nov 2007
Posts: 40
Location: Chennai

PostPosted: Sat Aug 17, 2024 12:58 am
Reply with quote

Hello,

I'm trying to pull data based on the difference between 2 date fields. If Exp_dte is greater than 1 year of Eff_dte then the query should produce the results. Could someone please assist me on this? I tried using DATEDIFF and also in where clause just put the difference between two dates but I either get "SQLCODE = -440, ERROR: NO AUTHORIZED FUNCTION NAMED DATEDIFF HAVING COMPATIBLE ARGUMENTS WAS FOUND" or "SQLCODE = -182, ERROR: AN ARITHMETIC EXPRESSION WITH A DATETIME VALUE IS INVALID"

Sample Table:
Code:

Clm_Num  Eff_dte           Exp_dte
A001         01/01/2023    01/01/2024
B002         01/01/2022    01/01/2023
C001         01/01/2021    01/01/2024
C002         01/01/2021    01/01/2023
D001         01/01/2023    01/01/2024
E001         12/01/2023    12/01/2024
E002         11/01/2022    11/01/2025


Required Output:
Code:

Clm_Num  Eff_dte           Exp_dte
C001         01/01/2021    01/01/2024
C002         01/01/2021    01/01/2023
E002         11/01/2022    11/01/2025
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2141
Location: USA

PostPosted: Sat Aug 17, 2024 1:44 am
Reply with quote

Please, present the real code of your attempts.
Back to top
View user's profile Send private message
Krishnadeva Reddy

New User


Joined: 14 Nov 2007
Posts: 40
Location: Chennai

PostPosted: Sat Aug 17, 2024 4:10 am
Reply with quote

Queries I tried:

SELECT Clm_Num, Eff_dte, Exp_dte from table1 where DATEDIFF(Exp_dte,Eff_dte) > 1 YEAR;

SELECT Clm_Num, Eff_dte, Exp_dte from table1 where
Exp_dte - Eff_dte > 1 YEAR;
Back to top
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2594
Location: Silicon Valley

PostPosted: Sat Aug 17, 2024 4:56 am
Reply with quote

re: "NO AUTHORIZED FUNCTION NAMED DATEDIFF HAVING COMPATIBLE ARGUMENTS WAS FOUND"

How are the two columns defined? And what are the acceptable arguements for DATEDIFF?
Back to top
View user's profile Send private message
Krishnadeva Reddy

New User


Joined: 14 Nov 2007
Posts: 40
Location: Chennai

PostPosted: Sun Aug 18, 2024 4:43 am
Reply with quote

Both columns are defined as DATE type.

Is there any other way to get the output through SQL?
Back to top
View user's profile Send private message
sergeyken

Senior Member


Joined: 29 Apr 2008
Posts: 2141
Location: USA

PostPosted: Mon Aug 19, 2024 12:34 am
Reply with quote

I posted the required syntax for DATEDIFF - as quoted from the User’s Guide, but my post has been deleted by moderators.
Back to top
View user's profile Send private message
Pedro

Global Moderator


Joined: 01 Sep 2006
Posts: 2594
Location: Silicon Valley

PostPosted: Mon Aug 19, 2024 7:59 am
Reply with quote

I forgot what Sergey had posted... but I found an example, something like this:

Code:
DATEDIFF(YEARS, Eff_dte, Exp_dte )


You might need to play with it a little to see what it returns.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts db2 vs static data COBOL Programming 1
No new posts Difference between joblib and steplib JCL & VSAM 2
No new posts Date format correction using dfsort DFSORT/ICETOOL 3
No new posts Sorting Date Field DFSORT/ICETOOL 4
No new posts External data queue (changes?) CLIST & REXX 0
Search our Forums:

Back to Top