If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Urgent-(DB2 Function)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-04-09, 22:18
thrm_t thrm_t is offline
Registered User
 
Join Date: Oct 2009
Posts: 1
Urgent-(DB2 Function)

I am writing a script in which I need to put a date function which will automatically generate the first business day of the current month and also the first business day of the last month(i.e. Monday). Its a script which will run report every month and I would need that function which would automatically pull up the business days as mentioned above.
Please advise.

SCRIPT:

-- first get list of servers added in the last month
with new_srvrs(physerial, invnum, phyhost, allocxdate) as (
select physerial, invnum, phyhost, allocxdate from inv_old_daily
where allocxdate='2009-08-03'
and INVNUM>(select max(INVNUM) from inv_old_daily where allocxdate='2009-07-01'))
,

Last edited by thrm_t; 10-04-09 at 22:27.
Reply With Quote
  #2 (permalink)  
Old 10-04-09, 22:38
Stealth_DBA Stealth_DBA is offline
Registered User
 
Join Date: May 2009
Posts: 472
thrm t, check out this link for
Scalar functions - IBM DB2 9.7 for Linux, UNIX, and Windows
(It if for DB2 LUW V9.7 since you didn't mention the DB2 version you are using or the operating system).

Specifically, look at NEXT_DAY and LAST_DAY (by adding 1 day you get the first of the next month). A combination of these with a little Date Arithmetic should get you the values you need. DAYOFWEEK may also be useful.
Reply With Quote
  #3 (permalink)  
Old 10-05-09, 00:29
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
Current month:

select
case
when dayofweek_iso (substr(current_date, 1,8)||'01') = 6
then substr(current_date, 1,8)||'03'
when dayofweek_iso (substr(current_date, 1,8)||'01') = 7
then substr(current_date, 1,8)||'02'
else substr(current_date, 1,8)||'01'
end
from sysibm.sysdummy1

Previous month:

select
case
when dayofweek_iso (substr(current_date - 1 month, 1,8)||'01') = 6
then substr(current_date - 1 month, 1,8)||'03'
when dayofweek_iso (substr(current_date - 1 month, 1,8)||'01') = 7
then substr(current_date - 1 month, 1,8)||'02'
else substr(current_date - 1 month, 1,8)||'01'
end
from sysibm.sysdummy1
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #4 (permalink)  
Old 10-05-09, 08:13
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
What about Mondays and Fridays that are your business's holidays? You might want your own calendar table.

Dave Nance
Reply With Quote
  #5 (permalink)  
Old 10-05-09, 13:18
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Here are two examples using date calculation on DB2.

"first biz day of month (1)" will work on DB2 9.7 for LUW and older.
"first biz day of month (2)" will work on DB2 9.7 for LUW only(LAST_DAY was used).
(I didn't check on other platform.)
Code:
------------------------------ Commands Entered ------------------------------
SELECT CURRENT_DATE AS CURRENT_DATE
     , CURRENT_DATE
       - (DAY(CURRENT_DATE)
          - SIGN( MOD( DAY(CURRENT_DATE) + 11 - DAYOFWEEK_ISO(CURRENT_DATE), 7 ) - 5 )
          - 2) DAYS
         AS "first biz day of month (1)"
     , CURRENT_DATE
       - (DAY(CURRENT_DATE)
          + SIGN( MOD( DAYOFWEEK_ISO(LAST_DAY(CURRENT_DATE - 1 MONTH)) + 2, 7 ) - 1 )
          - 2) DAYS
         AS "first biz day of month (2)"
  FROM sysibm.sysdummy1
;
------------------------------------------------------------------------------

CURRENT_DATE first biz day of month (1) first biz day of month (2)
------------ -------------------------- --------------------------
2009-10-06   2009-10-01                 2009-10-01                

  1 record(s) selected.
Reply With Quote
  #6 (permalink)  
Old 10-05-09, 17:35
yvanroy yvanroy is offline
Registered User
 
Join Date: Jun 2002
Posts: 12
Urgent-(DB2 Function)

in LUW 9.7...

Code:
select 
 current date as current_date
,next_day(last_day(current date - 1 month),'MONDAY') as first_monday_of_current_month
,next_day(last_day(current date - 2 month),'MONDAY') as first_monday_of_last_month
from sysibm.sysdummy1
; 

CURRENT_DATE FIRST_MONDAY_OF_CURRENT_MONTH FIRST_MONDAY_OF_LAST_MONTH
------------ ----------------------------- --------------------------
10/05/2009   10/05/2009                    09/07/2009                

  1 record(s) selected.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On