We help IT Professionals succeed at work.

CURRENT_DATE function to show prior month data

saved4use
saved4use asked
on
How do I use the CURRENT_DATE function in DB2 to pull data for the entire past month on the first day of a new month?
This job runs flawlessly daily with the piece of code below, but on the 1st of the month it returns no results:
ACCT_OPEN_DT >(CURRENT DATE - day(current date) days )

*What do I need to modify to make sure that on the 1st of each month, it still shows the prior month's entire data?
Thanks
Comment
Watch Question

--
so, on any day of the month you want results for this month ..

ie, when run on 13th of march, you want everything from 1st of march.

if you run on 1st of april, you want everything from 1st of march to 31st of march.

is my assumption right?

if it is, try


 ACCT_OPEN_DT >(case when day(current date)=1 then (CURRENT DATE - 1 month - 1 day) else (CURRENT DATE - day(current date) days ) end)

Open in new window

Dave FordSoftware Developer / Database Administrator
CERTIFIED EXPERT

Commented:
Let's see. CURRENT DATE - day(current date) days  will always return the last day of the previous month. If you wanted to return the FIRST day of the previouos month, you could do:

CURRENT DATE - day(current date) days + 1 day - 1 month

HTH,
DaveSlash
Dave FordSoftware Developer / Database Administrator
CERTIFIED EXPERT

Commented:
If you wanted to return the first day of the current month, you'd do this:

CURRENT DATE - day(current date) days + 1 day

Author

Commented:
@ sathyaram_s, You are correct, that's exactly what I want.
I've changed my code and will see what happens on 4/1.
Thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.