Skip to Content
Author's profile photo Sagar Girme

DS Custom Function to get last business date.

Hi All,

Below function will give the last business date – Ranging from Monday to Friday.

1. On Tuesday- Last business date will be Monday’s date

2. On Monday – Last Business date will be – Friday’s date

3.On Saturday and Sunday – Last business date will be- Friday’s date.

Function:

Name: FN_GET_BUSINESS_DATE( )

Example : FN_GET_BUSINESS_DATE( -1, Date Value)

# Comments
# Parameters
Input
#   $P_I_START_DATE   date
#   $P_I_DAYS2ADD     int
#
Output
#   Return     int
#   $P_O_RETURN_DATE        date
##
# Variables
#   $L_RETURN_DATE   date
#           $L_COUNTER              Int
#           $L_CUR_DATE             date
#           $L_DAYNUM               INT
#           $L_SKIPCNTR             INT
#           $L_DIRECTION            INT
#           $L_BUSINESSDAYS         INT
#
# Example : FN_GET_BUSINESS_DATE( -1, Date Value)
#
# ———————————–

Code:

           $L_COUNTER = 0 ;
           $L_CUR_DATE = $P_I_START_DATE;
           $L_SKIPCNTR = 0;
           $L_DIRECTION = 1;
           $L_BUSINESSDAYS = $P_I_DAYS2ADD;

If ($P_I_DAYS2ADD  < 0)
begin
    $L_DIRECTION = – 1;
    $L_BUSINESSDAYS = (-1) * $L_BUSINESSDAYS;
end

while ( $L_COUNTER < $L_BUSINESSDAYS )
begin
  $L_CUR_DATE= $L_CUR_DATE + $L_DIRECTION;
  $L_DAYNUM = day_in_week($L_CUR_DATE);
         
      if($L_DAYNUM >=  1 AND $L_DAYNUM <= 5)
           begin
                   $L_COUNTER = $L_COUNTER +1;
                 end
            else
        begin
                   $L_SKIPCNTR = $L_SKIPCNTR +1;
                 end
end
$L_RETURN_DATE = $P_I_START_DATE+ ($L_DIRECTION * ($L_COUNTER + $L_SKIPCNTR));

Return $L_RETURN_DATE;

——————————————————–

Thanks & Regards,

Sagar Girme

Assigned tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Asgar Ali MK
      Asgar Ali MK

      Good post Sagar,

      You could also get the same result using the below code

      L_CUR_DATE - decode( day_in_week(L_CUR_DATE)=1,3, day_in_week(L_CUR_DATE)=7,2,1);

      - Asgar

      Author's profile photo Former Member
      Former Member

      Good information. And good exmple to write custom function.

      Author's profile photo Former Member
      Former Member

      Thanks for the post Sagar, needed it badly

      Author's profile photo Former Member
      Former Member

      Good info.