View previous topic :: View next topic
|
Author |
Message |
aneeta
New User
Joined: 08 Dec 2005 Posts: 48 Location: Chennai
|
|
|
|
Hi,
My jobs runs on every month end..Eg Aug 31.I need to get the next month end using SQL query.Eg Sep 30 when it runs on Aug 31.
Basically get the next month end when the job runs for current month end.
Can someone pls help me with the query.This is a urgent requierement.
Thanks
Aneeta |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
Not a db2 solution, but is accurate and faster than using db2:
(stolen from Kolusu)
Code: |
01 WS-MONTH-END-DD PIC X(24) VALUE
'312831303130313130313031'.
01 WS-TBL-MONTH-END REDEFINES WS-MONTH-END-DD.
05 TBL-MONTH-END-DAY PIC 9(02) OCCURS 12 TIMES.
01 WS-GREG-DATE.
05 WS-GREG-YEAR PIC 9(04).
05 WS-GREG-MNTH PIC 9(02).
05 WS-GREG-DAY PIC 9(02).
EVALUATE TRUE
WHEN FUNCTION MOD (WS-GREG-YEAR 4) NOT ZERO
WHEN FUNCTION MOD (WS-GREG-YEAR 100) ZERO
AND FUNCTION MOD (WS-GREG-YEAR 400) NOT ZERO
MOVE '28' TO WS-TBL-MONTH-END (3: 2)
WHEN OTHER
MOVE '29' TO WS-TBL-MONTH-END (3: 2)
END-EVALUATE
MOVE TBL-MONTH-END-DAY(WS-GREG-MNTH)
TO WS-GREG-DAY
DISPLAY 'LAST-DATE OF MONTH:' WS-GREG-DATE
|
|
|
Back to top |
|
|
ulfnsc
New User
Joined: 15 Jan 2004 Posts: 19 Location: Stockholm, SWEDEN
|
|
|
|
What if the job run's in dec 2008 (20081231) ?? |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
He only asked for month end.
besides, it is not a db2 solution. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
Since Ulf pointed out that my routine sucked, how about using the last_day function:
Code: |
SELECT LAST_DAY('2008-01-01')
FROM SYSIBM.SYSDUMMY1
|
|
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
or even better. if your date is aug 31
Code: |
SELECT LAST_DAY(DATE('2008-08-31') + 1 MONTH )
FROM SYSIBM.SYSDUMMY1
|
of course you can do this with host variables |
|
Back to top |
|
|
aneeta
New User
Joined: 08 Dec 2005 Posts: 48 Location: Chennai
|
|
|
|
Hi,
I tried the last query and its throwing sysntax error.
Thanks
Aneeta |
|
Back to top |
|
|
Suresh Ponnusamy
Active User
Joined: 22 Feb 2008 Posts: 107 Location: New York
|
|
|
|
Hi
For me, Dick query is working fine and returning result. Please let us know the error you are getting? |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hello,
Quote: |
I tried the last query and its throwing sysntax error. |
Possibly a copy/paste or typing error.
As requested, please post the error as well as the query that gives the error (use copy/paste for both the query and the error). |
|
Back to top |
|
|
aneeta
New User
Joined: 08 Dec 2005 Posts: 48 Location: Chennai
|
|
|
|
Hi All.
This is the error message I get.
DSNE610I NUMBER OF ROWS DISPLAYED IS 0
DSNT408I SQLCODE = -20101, ERROR: THE FUNCTION LAST_DAY FAILED WITH REASON
00E73004
DSNT418I SQLSTATE = 56060 SQLSTATE RETURN CODE
DSNT415I SQLERRP = DSNXRLET SQL PROCEDURE DETECTING ERROR
DSNT416I SQLERRD = 1050 0 0 -1 0 0 SQL DIAGNOSTIC INFORMATION
DSNT416I SQLERRD = X'0000041A' X'00000000' X'00000000' X'FFFFFFFF'
X'00000000' X'00000000' SQL DIAGNOSTIC INFORMATION
DSNT417I SQLWARN0-5 = W,,,,, SQL WARNINGS
DSNT417I SQLWARN6-A = W,,,, SQL WARNINGS
I checked that the field LAST_DAY is not found in the SYSDUMMY1 Table.
Pls help me.
Thanks
Aneeta |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
what columns did you find in SYSIBM.SYSDUMMY1 ? |
|
Back to top |
|
|
aneeta
New User
Joined: 08 Dec 2005 Posts: 48 Location: Chennai
|
|
|
|
hI,
When I execute this query I get the output but not for last day.
SELECT CURRENT TIMESTAMP
FROM SYSIBM.SYSDUMMY1;
Pls help me resolve this.I need to complete this by THURDSAY.
Thanks
Aneeta |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
as Dick asked, please post your query......... this is thursday and it is your deadline, not ours. |
|
Back to top |
|
|
Srihari Gonugunta
Active User
Joined: 14 Sep 2007 Posts: 295 Location: Singapore
|
|
|
|
You can try the following query.
If you run this query on 31st August, the output will be 2008-09-30
SELECT (CURRENT DATE + 1 MONTHS) FROM SYSIBM.SYSDUMMY1; |
|
Back to top |
|
|
aneeta
New User
Joined: 08 Dec 2005 Posts: 48 Location: Chennai
|
|
|
|
SELECT LAST_DAY(DATE('2008-08-31') + 1 MONTH )
FROM SYSIBM.SYSDUMMY1;
DSNE610I NUMBER OF ROWS DISPLAYED IS 0
DSNT408I SQLCODE = -20101, ERROR: THE FUNCTION LAST_DAY FAILED WITH REASON
00E73004
DSNT418I SQLSTATE = 56060 SQLSTATE RETURN CODE
DSNT415I SQLERRP = DSNXRLET SQL PROCEDURE DETECTING ERROR
DSNT416I SQLERRD = 1050 0 0 -1 0 0 SQL DIAGNOSTIC INFORMATION
DSNT416I SQLERRD = X'0000041A' X'00000000' X'00000000' X'FFFFFFFF'
X'00000000' X'00000000' SQL DIAGNOSTIC INFORMATION
DSNT417I SQLWARN0-5 = W,,,,, SQL WARNINGS
DSNT417I SQLWARN6-A = W,,,, SQL WARNINGS
DSNE618I ROLLBACK PERFORMED, SQLCODE IS 0
DSNE616I STATEMENT EXECUTION WAS SUCCESSFUL, SQLCODE IS 0
DSNE601I SQL STATEMENTS ASSUMED TO BE BETWEEN COLUMNS 1 AND 72
DSNE620I NUMBER OF SQL STATEMENTS PROCESSED IS 1
DSNE621I NUMBER OF INPUT RECORDS READ IS 2
DSNE622I NUMBER OF OUTPUT RECORDS WRITTEN IS 24 |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
Back to top |
|
|
aneeta
New User
Joined: 08 Dec 2005 Posts: 48 Location: Chennai
|
|
|
|
Hi,
When I try thus query-
SELECT (DATE('2008-08-31') + 1 MONTHS )
FROM SYSIBM.SYSDUMMY1;
I get the result as 2008-09-30
But when I try this query for the next month end,
SELECT (DATE('2008-09-30') + 1 MONTHS )
FROM SYSIBM.SYSDUMMY1;
I get the result as 2008-10-30 instead of 2008-10-31.
Can I get help on this..I am try to complete this with version of DB2 i currently have as I am not able to use LAST_DAY function.
Thanks
Aneeta |
|
Back to top |
|
|
Craq Giegerich
Senior Member
Joined: 19 May 2007 Posts: 1512 Location: Virginia, USA
|
|
|
|
This will always return the first day of the current month
Code: |
SELECT CURRENT DATE - (DAY(CURRENT DATE) - 1) DAYS
FROM SYSIBM.SYSDUMMY1
|
This will always return the last day of the current month without using the last_day function
Code: |
SELECT CURRENT DATE - (DAY(CURRENT DATE) - 1 ) DAYS + 1 MONTHS - 1 DAYS
FROM SYSIBM.SYSDUMMY1;
|
For the last day of the next month without using the last_day function
Code: |
SELECT CURRENT DATE - (DAY(CURRENT DATE) - 1 ) DAYS + 2 MONTHS - 1 DAYS
FROM SYSIBM.SYSDUMMY1;
|
|
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19243 Location: Inside the Matrix
|
|
|
|
Hi Craig,
Cool
I deleted my earlier post about experimenting with some other dates.
Then i thought - will these also work for the earlier releases of db2?
d |
|
Back to top |
|
|
|