Creating a business hours function

I am new to oracle and I need to create a function that will take two datestamp and then tell me how many business hours are between those two dates.
Business hours are 6:00 Am to 6:00 pm Monday to Friday.
For example: if my startdate is 7/20/2010 8:00 am and end date us 7/2/2010 9:00 pm. The total work hour will be 10 hours not 13.
Another scenario:
start date: 7/20/2010 3:00 am and end time 7/20/2010 6:01 am. The total work hour is 1 min.
I am having serious difficulty with that.
Thanks
Amir

Hi, Amir,
user10356209 wrote:
Frank,
Thanks for your response. I really do not have a table that I need for this functiion. Right. The table is just for testing the function. Post a few rows to test.
CREATE OR REPLACE FUNCTION     business_hours
(      in_start_dt       IN          DATE     DEFAULT SYSDATE
,      in_end_dt       IN          DATE     DEFAULT     SYSDATE
RETURN     NUMBER
DETERMINISTIC
IS
--           **   b u s i n e s s _ h o u r s   **
--          business_hours returns the number of work houts (6 am through 6 pm,
--          Monday through Friday) between in_start_dt and in_end_dt.
--          If in_start_dt > in_end_dt, the results will be <= 0.
--          Holidays are not considered.
     d          NUMBER;                                -- Hours of either start_dt or end_dt after midnight
     end_dt          DATE    := GREATEST (in_start_dt, in_end_dt);  -- In case dates were in wrong order
     return_val     NUMBER;                                      -- Total number of working hours
     start_dt     DATE    := LEAST    (in_start_dt, in_end_dt);  -- In case dates were in wrong order
BEGIN
     WITH     all_days     AS
          SELECT     start_dt + LEVEL - 1     AS a_dt
          FROM     dual
          CONNECT BY LEVEL <= 1 + TRUNC (end_dt) - TRUNC (start_dt)
     SELECT     SUM (12)
     INTO     return_val
     FROM     all_days
     WHERE     TO_CHAR ( a_dt
               , 'Dy'
               , 'NLS_DATE_LANGUAGE = ''ENGLISH'''
               )       NOT IN ('Sat', 'Sun');
     -- Adjust hours from start_dt, if necessary
     IF  TO_CHAR ( start_dt
                  , 'Dy'
              , 'NLS_DATE_LANGUAGE = ''ENGLISH'''
              ) NOT IN ('Sat', 'Sun')
     THEN
          d := 24 * (start_dt - TRUNC (start_dt));
          IF  d >= 18
          THEN       -- Don't count start_dt itself
               return_val := return_val - 12;
          ELSIF  d > 6
          THEN    -- Don't count part of start_dt
               return_val := return_val - (d - 6);
          END IF;
     END IF;
     -- Adjust hours from end_dt, if necessary
     IF  TO_CHAR ( end_dt
                  , 'Dy'
              , 'NLS_DATE_LANGUAGE = ''ENGLISH'''
              ) NOT IN ('Sat', 'Sun')
     THEN
          d := 24 * (end_dt - TRUNC (end_dt));
          IF  d <= 6
          THEN       -- Don't count end_dt itself
               return_val := return_val - 12;
          ELSIF  d < 18
          THEN    -- Don't count part of end_dt
               return_val := return_val - (18 - d);
          END IF;
     END IF;
     IF  in_start_dt > in_end_dt
     THEN
          return_val := -return_val;
     END IF;
     RETURN     return_val;
END     business_hours
;Edited by: Frank Kulash on Jul 20, 2010 6:49 PM

Similar Messages

  • Performance issue in Store Business Document functionality in ME52N

    Hi Guys,
    I have a problem in the Services for object in the transaction ME52N.
    In Services for object
    Create->Store Business document functionality when i try to drag&drop the PDF type file it is taking longer time.
    Please provide some input to improve the performance.
    Regards,
    Ramesh

    Hi Ramesh,
    Could you explain the architecture of your DMS and ERP landscape.
    How these servers are placed ?
    Regards,
    Deepak Kori

  • Customer business hour week rule entries

    Dear experts,
    SAP CRM 7.0
    We are using the CRM customer business hours functionality so that calling times and visiting times for the customers can be defined in the system. We always call customers on different weekly frequencies like every week, every second week, every fourth week etc. but the list of these week frequnecies only has entries till every tenth week.
    However we need to add entries into it till 'every 24th week'. Could someone please help me know how this needs to be achieved?
    thanks & regards
    Ritwik Sharma

    That's terrible customer service.  Though my time is very valuable, I'm more angry that I was told that I would be credited when it wasn't true.  This was not my error, but Verizon's error and Verizon should take ownership of their mistake and honor what was said.  This is all in addition to 3 faulty phones and loss of service for days at a time.
    Sent from my iPhone

  • Any inbuilt Function Module available in CRM to create a Business Partner

    Hi All,
    I need to create a business partner in CRM not manually through GUI. I need to develop a function module that will create a Business Partner in CRM.
    Can some one tell me if there is an in-built BAPI/Function Module availabe in CRM to create a business partner in CRM.
    When i researched i found that there is a BAPI BAPI_BUPA_FS_CREATE_FROM_DATA2 which can create a business partner in CRM.
    I dont know how to use this BAPI, can some one help me with some sample code.
    Also will the development of a function module differ in CRM when compared to ECC?

    Hello,
    Standard CRM PC UI application offers three options when you push 'New' button: 'Person', 'Organization' and 'Group' (combobox appears). In your case, 'Person' should be selected.
    p.s. If you don't see combobox mentioned above, it could be security issue.
    Kirill

  • How to Calculate End-Date for a given Startdate in business hours without holidays

    Hello Experts,
    I need to create a plsql function where we need to determine end date for a task. For this, input will be only start date & number of hours allocated for that task. Based on this input we need to exclude Business hours & need to include only Business hours. and in the end we need to achieve the end date after the hour addition to the start date.
    Example : INPUT: 03-OCT-2013 12:00:00 PM /
                                  Hours Allocated 30 Hrs.
    Business Hours - 11 AM to 9 PM.
    So time starts from or Startdate is Friday 03-OCT-2013 12:00:00 PM. 
    So If I want to calculate the end-date by adding 30 hours to it, it should come WednesDay 09-OCT-2013 12:00:00 PM because I excluded Weekends & considered only business hours that is 11 am to 9 pm.
    I am not able to get any such guidance in Internet as most of the docs are having start & end date as input.
    Please help!
    Thanks in advance !!

    Hi,
    As Christ said, there's no nuilt-in Oracle function to tell whether a given DATE is a holiday or not, partially because there's so much local variation in holidays.  You can write a function like that (see http://forums.oracle.com/forums/message.jspa?messageID=3351081 ), but creating a table will be simpler and more efficient.
    I suggest creating a row for every date, whether it's a work day or not; that way, you can have variations in the schedule (e.g., schedule changes and
    partial holidays).
    CREATE TABLE  work_calendar
    (   dt  DATE       PRIMARY KEY
                       CONSTRAINT  work_calendar_dt
                           CHECK (dt = TRUNC (dt))
    ,   day_type       VARCHAR2 (8)  NOT NULL
                       CONSTRAINT  work_calendar_day_type
                           CHECK ( day_type IN ( 'HOLIDAY'
                                               , 'WEEKEND'
                                               , 'WORK DAY'
    ,   start_time     DATE
    ,   end_time       DATE
    ,   work_hours     NUMBER
    ,   work_hours_since_1970     -- or some point earlier than you'll ever need
                       NUMBER
    ,   CONSTRAINT  work_calendar_start_time
            CHECK ( TRUNC (start_time) = dt)
    ,   CONSTRAINT  work_calendar_end_time
            CHECK ( TRUNC (end_time) = dt)
    This will let you do a lot of the calculations you need without a function.

  • SLA Calculation needed to exclude Holidays, Weekends and Off business hours

    Hello all,
    I am to create a function that will calculate the minutes between two dates for company work hours, excluding the weekends. 
    The function here from RSingh() is ideal for this purpose; however I am now asked to excluded company holidays also.
    http://social.msdn.microsoft.com/Forums/sqlserver/en-US/de4f7661-c702-4a10-ad06-3e28e4e0a83c/sla-calculation-help-needed-in-excluding-weekends-and-off-business-hours-for-calculation?forum=transactsql
    With a table called 'holidays' that contains holiday dates. I would hope to modify the function to exclude these.
    Please, how can this be accomplished? 

    My approach would be something like this
    Populate a calendar table that includes a work start and work end time for each day (none for weekends and holidays)
    SELECT *
    INTO #cal
    FROM (
        SELECT CAST('20140117' AS DATE) AS CalendarDate
            , CAST('20140117 09:00' AS DATETIME) WorkStart
            , CAST('20140117 17:00' AS DATETIME) AS WorkEnd
        UNION ALL
        SELECT CAST('20140118' AS DATE) AS CalendarDate
            , CAST(NULL AS DATETIME) WorkStart
            , CAST(NULL AS TIME) AS WorkEnd
        UNION ALL
        SELECT CAST('20140119' AS DATE) AS CalendarDate
            , CAST('20140119 09:00' AS DATETIME) WorkStart
            , CAST('20140119 17:00' AS DATETIME) AS WorkEnd
        UNION ALL
        SELECT CAST('20140120' AS DATE) AS CalendarDate
            , CAST(NULL AS DATETIME) WorkStart
            , CAST(NULL AS TIME) AS WorkEnd
        UNION ALL
        SELECT CAST('20140121' AS DATE) AS CalendarDate
            , CAST(NULL AS DATETIME) WorkStart
            , CAST(NULL AS TIME) AS WorkEnd
        UNION ALL
        SELECT CAST('20140122' AS DATE) AS CalendarDate
            , CAST('20140122 09:00' AS DATETIME) WorkStart
            , CAST('20140122 17:00' AS DATETIME) AS WorkEnd
        ) X
    Use a query something like the following to work out the number of minutes
    DECLARE @Start DATETIME = '20140117 13:00'
    DECLARE @End DATETIME = '20140122 17:00'
    SELECT SUM(diff)
    FROM (
    SELECT DATEDIFF(MINUTE, CASE WHEN CAST(@Start AS DATE) = CalendarDate
    AND @Start > WorkStart THEN @Start ELSE WorkStart END, CASE WHEN CAST(@End AS DATE) = CalendarDate
    AND @End < WorkEnd THEN @End ELSE WorkEnd END) AS diff
    FROM #cal
    WHERE CalendarDate BETWEEN CAST(@Start AS DATE)
    AND CAST(@End AS DATE)
    ) X
    Edit: Totally mucked up the example calendar table so I've changed it

  • I'm looking for a voicemail app that will automatically switch over after normal business hours and play an alternate voice-mail message. Does this currently exist?

    I’m looking for a voicemail app that will automatically switch over after normal business hours and play an alternate voice-mail message. Does this currently exist?

    Voicemail is through your carrier and on their equipment.
    It's function and features is set by them, not your phone.

  • CAM_ERROR while creating a Business Partner using SD_CUSTOMER_MAINTAIN_ALL

    Hello All,
    I was trying to create a Business Partner(Ship to Party) using function module SD_CUSTOMER_MAINTAIN_ALL. When I am executing this through SE37 transaction, its working fine &
    creating a KUNNR value along with new ADRNR number.
    But when the same data is passed inside the program, its throwing CAM_ERROR(sy-subrc = 22).
    If anyone used this FM, please suggest. Thank you.

    Hello Ragu,
    I am passing the data to function module as shown below.
    *KNA1 population
                      ls_kna1-mandt = sy-mandt.
                      ls_kna1-ktokd = 'ZN02'.
                      ls_kna1-land1 = ls_adrc-country.
                      ls_kna1-name1 = ls_adrc-name1.
                      ls_kna1-name2 = ls_adrc-name2.
                      ls_kna1-ort01 = ls_adrc-city1."City
                      ls_kna1-regio = ls_adrc-po_box_reg.
                      ls_kna1-pstlz = ls_adrc-post_code1.
                      ls_kna1-sortl = ls_adrc-sort_phn.
                      ls_kna1-stras = ls_adrc-house_num1.
                      ls_kna1-spras = ls_adrc-langu.
    *BAPIADDR1 population
                      ls_bapiaddr1-name          = ls_adrc-name1.
                      ls_bapiaddr1-name_2        = ls_adrc-name2.
                      ls_bapiaddr1-city          = ch_adrc_struc-city1.
                      ls_bapiaddr1-postl_cod1    = ch_adrc_struc-post_code1.
                      ls_bapiaddr1-street        = ch_adrc_struc-street.
                      ls_bapiaddr1-house_no      = ch_adrc_struc-house_num1.
                      ls_bapiaddr1-str_suppl1    = ch_adrc_struc-str_suppl1.
                      ls_bapiaddr1-str_suppl2    = ch_adrc_struc-str_suppl2.
                      ls_bapiaddr1-location      = ch_adrc_struc-location.
                      ls_bapiaddr1-country       = ch_adrc_struc-country.
                      ls_bapiaddr1-langu         = ch_adrc_struc-langu.
                      ls_bapiaddr1-sort1         = ch_adrc_struc-sort1.
                      ls_bapiaddr1-sort2         = ch_adrc_struc-sort2.
                      ls_bapiaddr1-langu_cr      = ch_adrc_struc-langu_crea.
    *KNVV Population
                      ls_knvv-vkorg              = '1000'.
                      ls_knvv-vtweg             = '01'.
                      ls_knvv-spart               = '01'.
                        CALL FUNCTION 'SD_CUSTOMER_MAINTAIN_ALL'
                              EXPORTING
                                i_kna1                        = ls_kna1
                                i_knvv                        = ls_knvv
                                i_bapiaddr1                   = ls_bapiaddr1
                                i_maintain_address_by_kna1    = 'X'
                               i_no_bank_master_update       = 'X'
                               i_raise_no_bte                = 'X'
                                pi_postflag                   = 'X'
                               i_from_customermaster         = 'X'
                              IMPORTING
                                e_kunnr                       = lv_kunnr
                                o_kna1                        = ls_kna1_output
                              EXCEPTIONS
                                client_error                  = 1
                                kna1_incomplete               = 2
                                knb1_incomplete               = 3
                                knb5_incomplete               = 4
                                knvv_incomplete               = 5
                                kunnr_not_unique              = 6
                                sales_area_not_unique         = 7
                                sales_area_not_valid          = 8
                                insert_update_conflict        = 9
                                number_assignment_error       = 10
                                number_not_in_range           = 11
                                number_range_not_extern       = 12
                                number_range_not_intern       = 13
                                account_group_not_valid       = 14
                                parnr_invalid                 = 15
                                bank_address_invalid          = 16
                                tax_data_not_valid            = 17
                                no_authority                  = 18
                                company_code_not_unique       = 19
                                dunning_data_not_valid        = 20
                                knb1_reference_invalid        = 21
                                cam_error                     = 22
                                OTHERS                        = 23.
                            IF sy-subrc = 0.
                              EXPORT kunnr FROM lv_kunnr TO MEMORY ID 'Z_CUST_NUM'.
                              EXPORT adrnr FROM lv_adrnr TO MEMORY ID 'Z_ADDR_NUM'.
                            ENDIF.
    When I am executing this through SE37, its working perfectly. However when used in Sales Order Save exit, its throwing an error saying SY-Subrc = 22(CAM_ERROR).

  • RE:Business partner functions in customer Master creation

    Do we need to send the Business partner functions while creating record in R3 thru MDM.
    For any account group i pass the Idoc is expecting all Business partner functions to be defined for the account group to which am trying to create Customer.(As SH,BP,PY..)
    Can anyone please explain me in brief the Business partner function -Problem in my scenerio............................Concept of  Business Partner function while creating Customer.

    In ECC partner functions are used when you have multiple addresses for the same customer.  The partner function links those address records together.  You could have multiple unique customer numbers on the KNA1 table, but in the KNVVP table (partner functions) you maintain the linkage between the main table records. 
    Partner functions are not recommended in MDM.  These are generally not considered to be "enterprise level attributes" and therefore shouldn't reside in your customer repository in a central create scenario.  Additionally, some of the complexity required to model partner functions can't be modeled in MDM at this time.  When 7.1 comes out, there is a new data type called "tuples" which supports deeply nested structures, and therefore you could model a true partner function table inside the sales area table.

  • Use of business partner functionality in combination with dual control

    Is there a generally agreed upon procedure to use the business partner functionality in combination with dual control? The problem is that when you block a business partner, the customer/ vendor master data aren't blocked automatically. You can then still use them in transactions, which leads to problems.
    So is there a way to make this work or a procedure to do this?
    Niels Vanwingh

    Thanks Masa,
    I did notice the 'Add external supplier from' in create supplier or bidder option. However there is a small catch and your experience may help.
    Let me explain the requirement and scenario here in SRM 7.
    We are implementing the Registration of Supplier scenario; both ROS and SRM are in same client. When a potential supplier registers themselves in the registration system, a BP number is created (an Internal number range is defined for this). After accepting the potential supplier in pre-select screen the purchaser has two options to transfer the potential supplier from the ROS system to SRM
    Option 1: He can select the accepted potential supplier from the supplier directory option and transfer the business partner to SRM. In this case the business partner number of the potential supplier is retained in SRM and a business partner with supplier and bidder tag is created. However the purchaser does not have any option to select which type of business partner he would like to create like supplier or bidder.
    Option 2: Purchase can go to create supplier or bidder option and choose the u2018Add external supplier formu2019 from the ROS system and create the business partner. The ROS business partner details are copied to the create supplier screen, but the purchaser have to provide an external business partner number for the supplier. This is because we have defined external number range for business partner for the vendors that are replicated from ERP to SRM.
    Objective is the ROS business partner should be retained in SRM with option to create as supplier and bidder and then manually create ERP supplier with same SRM BP number and map against SRM supplier.
    Is there any way we can achieve this?
    In SRM 5.5 with Manage business partner functionality we could achieve as system give us the option which business partner type we would like to create as well as retains the ROS BP number in SRM.
    Regards
    Sandeep

  • Using a uddi key to create a business service in OSB

    I want to be able to use a service registry key when creating a business service in OSB. I've found documentation on how to do this in Oracle ESB, but not in OSB.
    From the documentation for OSB, I can only find a way to synchronize the business services imported from the service registry (when i search through the imported services there is no mention of the uddi key that i can see). I can choose to auto-import when setting up the registry, but I'm not sure exactly what this behavior does.
    From the console documentation (http://download.oracle.com/docs/cd/E13159_01/osb/docs10gr3/consolehelp/uddi.html)
    "You can use the Auto-Import Status page to synchronize changes to a service with those present in the registry. Upon any changes to a service in the registry, Oracle Service Bus provides notification of the change on the Auto-Import Status page which lists all out-of-sync services. You can then synchronize the service in the Oracle Service Bus Console with the corresponding service in the UDDI registry."
    This setting seems to automatically update the endpoint of the business service wihtout that administrator having to do anything, but what is actually being done? Is the service bus updating the endpoint as soon as it has been notified of a change or when i view/call the service is the endpoint being looked up?
    Am I missing something??
    Edited by: user498458 on Apr 27, 2009 11:30 AM

    I took your advice and re factored the Receive class so it had all the functionality I needed except two methods which would then be overridden in the derived classes. What I had a hard time understanding is once an object of class say SpecialReceive extends Receive it can be referred to as Receive yet still maintain that it's a SpecialReceive.
    What I mean is if I give a Receive object to some method and this method has no idea that this object is actually SpecialReceive and it calls a method say run() which gets overridden by the SpecialReceive. I thought that since this method has no idea what type of Receive this object is it will execute Receive's run(). But it actually does know what type of class it is and it executes the right run() method (the one that SpecialReceive overrides).
    After I realized this there is no more need for casting.
    Thanks everyone for your contributions.

  • Help with Creating a Business Rule...

    Hi All,
    I am using Planning 11.1.1.1.0.
    I have created a classic planning application and now have to incorporate a way to copy data from one slice of the essbase db to other.
    I am trying to copy data from the following slice
    Entity : EasternZone
    Version:Working (Bottom Up)
    Scenario: Actuals
    Currency:USD
    Period:YearTotal
    Year: 2007
    to
    Entity : EasternZone
    Version:Working (Bottom Up)
    Scenario: Actuals
    Currency:USD
    Period:YearTotal
    Year: 2008 and increase by a certain percentage which is not fixed..
    How should i go about achieving this using business rules? (Calc manager is used with only EPMA applications so where do i create the business rule?) Can somebody share some ideas please?
    Any help would be greatly appreciated.
    Kind Regards.
    Alicia

    Hi!
    Are you transferring data in same application from db to another? If yes, then possibly you could use @xref function.
    for example :
    fix ("EasternZone", "Working","Actuals","USD","YearTotal")
    "2008" = @xref("_name of source db location alias_","2007")*[RunTimePrompt for percentage];
    endfix
    The name of the location alias you can find from destination db --> edit --> location aliases. There you can see the location alias name of the source db.
    if you want to be sure about block creation then you could also add the following rows at the beginning
    /*fix ("EasternZone", "Working","Actuals","USD","YearTotal","any account")*/
    /* "2008" = 0;*/
    /*endfix*/
    kind regards,
    user637777

  • How to create the change document functionality for a dependent objects?

    May I please know how to create the change document functionality for a dependent objects?
    I have done it follow the same process as for business process objects. But when i try to test it in BOBT, there is no records under "FIELD_CHANGE_WITH_FILTER". It seems the change hasn't been recorded.
    If the way I did to create change document for dependent object is correct, please also kindly advise the possibilities for why there is no record during testing.
    thanks in advance.

    I also have some doubts about the business object.In this case,one abstract BO hase a subnode wihich is root extended.I added the change document for this node under the category "root_extended" and then tested the function in BOBT.I got some error message "can't find the root key".Shall i redefine the method /BOFU/IF_CDO_CREATION~IS_CDO_CREATION_ACTIVE so as to solve the problem?
    Thx.

  • IS- Retail - Cycle count during business hours

    Hi ,
      I am trying to do cycle count thru the POS system, and this is during business hours.  We want to perform cycle count at the stores using the POS system and update SAP.  I checked in SAP Help and its very confusing and its not step by step.  The config path provided in SAP Help also is not updated.  Is there any documents or materials that anyone of you can provide me.  Any information provided is appreciated.
    Thanks,
    Amauris.

    Hi,
    SAP has introduced this new functionality called Physical Inventory during Opening hours along with EhP3 along with POS DM. Which means in case you want to carry out physical inventory during business hours, you will require POSDM as well as EHP3 installed as well as relevant Business function set activated to carry this out.
    In case you do not have POSDM in your landscape i dont think you can do it.
    Hope this helps
    Regards
    Manish

  • How to create a business partner whose BP category is person in crm portal?

    How to create a business partner whose BP category is person in crm portal.When I use Partner and account management>Partner function in portal,I create a new partner,the partner category is default organization,can not change.
    But I what to create a partner with the partner category person,how can I do it?
    Is there any other special function to ceate a BP in portal matching the function  with T code Bup1(create BP)in sap gui?
    By the way,I have the whole privileges in portal and crm.
    thanks

    Hello,
    Standard CRM PC UI application offers three options when you push 'New' button: 'Person', 'Organization' and 'Group' (combobox appears). In your case, 'Person' should be selected.
    p.s. If you don't see combobox mentioned above, it could be security issue.
    Kirill

Maybe you are looking for

  • ITunes Upgrade results in no access to ITunes Store

    I've recently downloaded the latest version of Itunes (9.2.1.5) as since doing so I cannot access the ITunes Store. Prior to downloading this laterst version, I had no trouble with access. I've tried deleting the download and re-installing it, and I'

  • RPS 675 UK AC Power Cord

    We have recently moved office and our RPS 675's have ended up connected with an AC power cord with a 5A fuse. The unit itself is rated at 10-6A so I'm thinking the power cords have been mixed up and should have a higher rated cable (like 10Amp). Unfo

  • Data Transfer Erec and ECC HR

    Hi all, We are using having 2 backend systems 1 for HR other than Erec and another one for erec. Now we have setup the ALE data transfer using Message type HRMD_ABA. In erec we have only ERECRUIT Component deployed in backend,no EA HR or SAP HR compo

  • Ebcdic to ascii file translation woes

    I am trying to convert an ebcdic file into an ascii file but i am running into some difficulties. The translation works fine, but it outputs everything onto one line when opened up in notepad on NT, with a symbol "ٱ" where a new line should be. When

  • Load external PDF JobOptions

    Hi All, Is it Possible to load pdf export presets? Trying Script: var mypdfExportPresets = app.pdfExportPresets.everyItem().getElements(); alert("mypdfExportPresets: " + mypdfExportPresets.length); for(i=0; i<mypdfExportPresets.length; i++)     var m