Sorry, you do not have permission to ask a question, You must login to ask a question.

Sorry, you do not have permission to ask a question.

brainchime.com

brainchime.com

brainchime.com Navigation

  • Home
  • About Us
  • Contact Us

Mobile menu

Close
  • Home
  • Categories
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags

brainchime.com Latest Questions

Admin
Admin
Asked: October 6, 20242024-10-06T07:06:00+05:30 2024-10-06T07:06:00+05:30In: SQL

How to create an SQL function for calculating the number of business days between two dates?

I want to create an SQL function for calculating the number of business/working days between two dates, similar to the NETWORKDAYS function for Microsoft Excel.

This SQL function should accept two input parameters such as start date and end date of type DATE and should return the number of business/working days in integer format.

How should I create this SQL function and run it on IBM i?

as400db2sql-function
  • 0
  • 0
  • 11
  • 7
  • Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp

Related Questions

  • what is level check error and how to resolve level check error?
  • What are AS400 systems. Please provide a introduction of AS400 systems?
  • Is there any online server available for practicing on AS400 system?
  • What are the menus available in AS400?
  • What is subsystem in AS400?
  • How to copy a save file from IFS to a library?
  • How to transfer savf from as400 to pc?
  • How to copy ifs file to another directory?
  • How to copy savf from ifs to pc?
  • How to copy ifs file to physical file?
  • How to copy save file to ifs?
  • How to copy spool file to ifs?
  • How to copy physical file to ifs?
  • How to copy file from pc to IFS?
  • How to download spool file from AS400?
  • How do I delete a library in AS400?
  • How do I copy data from AS400 to excel?
  • What is library in AS400?
  • How to find all the source physical file available in AS400?
  • How to find all libraries in AS400?
  • How to change the library list in AS400?
  • What is access path in AS400?
  • What is the difference between source physical file and physical file in as400?
  • how to find the source file of an object in as400?
  • how to change record length of source physical file in as400?
  • What is cpf4174 error in as400?
  • What is the use of varying keyword in rpgle?
  • What is DDS in AS400?
  • What is the difference between PF and LF in as400?
  • Why do we use CHGPF command in AS400?
  • how to create physical file in as400?
  • What are the data types supported by physical files in AS400?
  • how to add data in physical file in as400?
  • how to view journal entries in as400?
  • what is the use of ovrdbf in as400?
  • What is an array in AS400?
  • what is a data queue in as400 and why do we use data queue?
  • How to run stored procedure in AS400?
  • How to resolve session and device error in AS400?
  • how to check as400 system values?
  • How to check triggers on a file in as400?
  • How to find damaged objects in AS400?
  • what is module in as400?
  • How to create binding directory in as400?
  • how to create ifs folder in as400?
  • What is ASP in AS400?
  • What is JOBQ and how to create a JOBQ in AS400?
  • What is PSDS in AS400?
  • What is SEU in AS400 and why do we use it?
  • What is the multi-format logical file in AS400?
Leave an answer

Leave an answer
Cancel reply

Browse
Browse

Choose from here the video type.

Put Video ID here: https://www.youtube.com/watch?v=sdUUx5FdySs Ex: "sdUUx5FdySs".

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Admin
    Admin
    2024-10-06T07:29:54+05:30Added an answer on October 6, 2024 at 7:29 am

    Using SQL we can create a user-defined function (UDF) for calculating the number of business/working dates between two dates, similar to the NETWORKDAYS function for Microsoft Excel.

    Creating the SQL function:
    We can execute the following SQL code to create our SQL function. You can either run the below scripts from the Run SQL script option from IBM i Access client solutions (ACS) or on IBM i green screen, write the script within a source member of source type SQL at the location libname/srcpfname and then use RUNSQLSTM SRCFILE(libname/srcpfname) SRCMBR(srcmbrname) COMMT(*NONE) DFTRDBCOL(libname) DBGVIEW(*SOURCE) command to execute this script to create SQL function.

    create or replace function libname.workingday(startdate DATE,
    enddate DATE)
    returns INTEGER
    language SQL
    is deterministic
    begin
      declare workingday INTEGER default 0;
      declare tempdate DATE;
      set tempdate = startdate;
      while days(tempdate) <= days(enddate) do
        if dayofweek_iso(tempdate) <> 6 and
           dayofweek_iso(tempdate) <> 7 then
          set workingday = workday + 1;
         end if;
         set tempdate = date(days(tempdate) + 1);   
      end while;
      return workingday; 
    end; 

    Explanation of the code:
    Line 1 -2: Create and replace function workingday that accepts two parameters startdate and enddate of type date.
    Line 3: The SQL function returns an integer value which is the number of business/working days.
    Line 4-5: Language is SQL and it is deterministic i.e. it returns the same value on the same input dates each time.
    Line 6: begin of the function
    Line 7: Declare workingday variable of type integer and initialize with value 0.
    Line 8: Declare tempdate variable of type date.
    Line 9: set the value of tempdate as startdate.
    Line 10-16: Run the while loop until days of tempdate <= days of enddate. Days() function returns the integer representation of the date i.e. for Jan 1, 0001(0001-01-01) it returns 1, for (0001-01-02) it returns 2 and so on. Then we check if tempdate is not the 6th and 7th day of the week using DAYOFWEEK_ISO() function then we set the workingday as workingday + 1 i.e. increment by 1 and no increment when tempdate is the 6th and 7th day of the week. After end if; we increment the tempdate by 1 i.e. increment the number of days extracted from tempdate and convert back to date date(days(tempdate) + 1)and while loop end.
    Line 17: Return the workingday value.
    Line 18: end of function.
    Important Point:
    Once the function was created an object of the type *SRVPGM and attribute CLE was created in the specified library.

    How to call SQL function from Run SQL script ACS or STRSQL session on IBM i:

    select libname.workingday('2024-09-05','2024-09-10) from sysibm.sysdummy1
    

    the number of business/working days returned is 4. Since the 5th is Thursday and the 10th is Tuesday and there are total days in between these two dates are 6 and non-working days are 2 i.e. 7th and 8th (Saturday and Sunday) therefore working days are only 4.

      • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp

Sidebar

Statistics

  • Questions 143
  • Answers 177
  • Comments 0
  • Popular
  • Answers
  • Admin

    Why do we use OVERLAY keyword in data structure subfields ...

    • 12 Answers
  • Admin

    How to call sql stored procedure with output parameter from ...

    • 6 Answers
  • Admin

    How to use declare global temporary table statement in RPGLE?

    • 5 Answers
  • Admin
    Admin added an answer CPF4131 is a record format level check error. This indicates… October 18, 2024 at 1:58 am
  • Admin
    Admin added an answer To open the command prompt with administrator rights you can… October 17, 2024 at 12:27 am
  • Admin
    Admin added an answer In AS400, "AS" stands for Application system. This article discusses… October 13, 2024 at 12:49 pm

Related Questions

  • Admin

    what is level check error and how to resolve level ...

    • 1 Answer
  • Admin

    What are AS400 systems. Please provide a introduction of AS400 ...

    • 1 Answer
  • Admin

    Is there any online server available for practicing on AS400 ...

    • 1 Answer
  • Admin

    What are the menus available in AS400?

    • 1 Answer
  • Admin

    What is subsystem in AS400?

    • 1 Answer

Trending Tags

.htaccess (1) as400 (123) bing-webmaster (2) control-language (12) db2 (33) ftp (8) google-adsense (1) google-search-console (3) https-redirect (1) iasp (4) ifs (22) jar (4) operations (3) php-my-admin (1) qshell (3) robots.txt (4) rpg (26) stored-procedure (3) stroed procedure (1) triggers (1) yoast (4)

Explore

  • Home
  • Categories
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags

Footer

BrainChime

BrainChime is a blog that posts question-and-answer-based format articles on diverse topics and engages in discussions by allowing people to provide answers/comments without the need to register and log in.

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • FAQs
  • Categories
  • Tags

© 2024 BrainChime. All Rights Reserved
by BrainChime.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.