Exclude weekend query

Hi expert,
I already create udf for date start( u_start ) and date end ( u_end ) column in SO,i just want to know how to create simple query that can exclude weekend like saturday and sunday, example from 2/5/2011 until 25/11/2011 has 24 days,so i want minus all weekend like saturday and sunday,so i can get 18 day,can someone help me about this......

Hi Pari,
i want to create in query only not in crystal report,in sales order
Hi Gordon,
Its just calculation only right?  this example query i try
SELECT DATEDIFF(dd,u_start,u_end) - DATEDIFF(dd,u_start,u_end)*2/7 'Days without weekend' from ordr where docnum = 281
when i choose start date 1/12/2011 and end date 4/12/2011 it calculate 3 days,it suppose to be 2 days if without saturday and sunday as weekend
thanks for your help
regards,
Arif

Similar Messages

  • Query - Invoices posted yesterday excluding weekends. Drill arrows missing.

    Hi All,
    I have a working query that returns all AR Invoices posted Yesterday excluding weekends.
    There are however no drill down yellow arrows in the result for DocNum. Is there a way I can restructure my query so the drill own arrows appear?
    declare @dt_today datetime,
                    @dt_yesterday datetime
    select @dt_today = DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 0)
    select @dt_yesterday = case DATEPART(DW,@dt_today)
            when 2 then DATEADD(dd, -3, @dt_today)
            when 1 then DATEADD(dd, -2, @dt_today)
            else DATEADD(dd, -1, @dt_today) end
    SELECT T0.[DocNum] As 'Invoice number', T0.[DocDate] As 'Posting Date',
    T0.[DocTotal] As 'Invoice Total', T0.[GrosProfit] As 'Gross Profit',
    ((T0.[GrosProfit] / (T0.[DocTotal] - T0.[GrosProfit]))*100) As 'Profit %',
    T1.[CardCode], T1.[CardName], T0.[NumAtCard] As 'Customer PO#', T2.[GroupName],
    T1.[Phone1], T1.[CntctPrsn]
    FROM OINV T0 INNER JOIN OCRD T1 ON T0.CardCode = T1.CardCode INNER JOIN OCRG
    T2 ON T1.GroupCode = T2.GroupCode WHERE T0.[DocDate] = @dt_yesterday

    Hi,
    Try this way:
    SELECT T0.DocNum  As 'Invoice number', T0.DocDate As 'Posting Date',
    T0.DocTotal As 'Invoice Total', T0.GrosProfit As 'Gross Profit',
    ((T0.GrosProfit / (T0.DocTotal - T0.GrosProfit))*100) As 'Profit %',
    T1.CardCode, T1.CardName, T0.NumAtCard As 'Customer PO#', T2.GroupName,
    T1.Phone1, T1.CntctPrsn
    FROM OINV T0 INNER JOIN OCRD T1 ON T0.CardCode = T1.CardCode INNER JOIN OCRG
    T2 ON T1.GroupCode = T2.GroupCode
    WHERE DATEDIFF(DD, T0.DocDate, GetDate()) = 1 OR (DATEDIFF(DD, T0.DocDate, GetDate()) = 3 AND DATEPART(dw,GetDate()) in (1,2))
    If still  no arrow, add For Browse.
    Thanks,
    Gordon

  • Excluding Weekend dates from existing query

    I have the following query that returns the average of the total sum between start and end date of a status.
    How do I take out the weekend dates so it doesn't include saturday and sunday.?
    SELECT TRUNC(AVG( SUM ( (DECODE (status, 'Estimator Notified' ");
    ,cc.end_date,NULL) - DECODE (cc.status, 'Estimator Notified',cc.start_date,NULL)))),1)
    FROM cc_request_status cc
    group by cc.request_id

    The above solution actually generates each date in a range and tests it for a Saturday or Sunday date. A more efficient solution calculates the weekend days directly by using the following facts:
    1) For each whole week in the range the number of weekends days is 2.
    2) For the partial week, a function can be made based on the ending day and the number of partial days to calculate the number of weekend days.
    The following code uses the above two facts to calculate the number of days in a range excluding weekends:
    SELECT NUM_DAYS - (WEEKS*2) -
           CASE WHEN REMAINDER = 0 THEN 0
                WHEN END_DOW = 7 THEN 1
                ELSE
                    CASE
                        WHEN END_DOW > REMAINDER THEN 0
                        WHEN END_DOW = REMAINDER THEN 1
                        ELSE 2
                    END
           END BUS_DAYS
    FROM
        SELECT END_DOW, NUM_DAYS, FLOOR(NUM_DAYS/7) WEEKS, MOD(NUM_DAYS,7) REMAINDER
        FROM
            SELECT (TO_NUMBER(TO_CHAR(END_DATE, 'J')) -
                    TO_NUMBER(TO_CHAR(START_DATE, 'J'))) + 1 NUM_DAYS,
                   TO_NUMBER(TO_CHAR(END_DATE,  'D')) END_DOW
            FROM
              SELECT TO_DATE('05/05/2007','MM/DD/YYYY') START_DATE,
                     TO_DATE('05/14/2007','MM/DD/YYYY') END_DATE
              FROM DUAL
    );You can collapse some of the inline views down, I expanded them to show each step.
    JR

  • Add working days to a date excluding weekends and holidays

    Hi there,
    I need to write a function that will take a specified date and number of days to add as input parameters, and return the working day based on the number of days to add parameter, excluding weekends and any holidays held in a holiday table.
    Here is my function so far -
    CREATE OR REPLACE FUNCTION f_add_work_days(pd_date IN DATE
    ,pn_add_days IN PLS_INTEGER) RETURN DATE IS
    pd_in_date DATE := pd_date;
    ld_next_holiday DATE;
    ln_days_left PLS_INTEGER := pn_add_days;
    CURSOR cu_holiday(pn_date IN ge740.holdte%TYPE) IS
    SELECT pck_utility.f_dtcnv(g.holdte)
    FROM ge740 g
    WHERE g.holdte >= pn_date
    AND g.maint <> 'D'
    ORDER BY g.holdte ASC;
    BEGIN
    OPEN cu_holiday(pck_utility.f_dtcnv(pd_in_date));
    FETCH cu_holiday
    INTO ld_next_holiday;
    CLOSE cu_holiday;
    LOOP
    IF ln_days_left = 0 THEN
    EXIT;
    END IF;
    pd_in_date := pd_in_date + 1;
    IF pd_in_date > ld_next_holiday THEN
    OPEN cu_holiday(pck_utility.f_dtcnv(pd_in_date));
    FETCH cu_holiday
    INTO ld_next_holiday;
    CLOSE cu_holiday;
    END IF;
    CASE
    WHEN TO_CHAR(pd_in_date
    ,'fmDAY') = 'SATURDAY' THEN
    pd_in_date := pd_in_date + 2;
    ln_days_left := ln_days_left - 1;
    WHEN TO_CHAR(pd_in_date
    ,'fmDAY') = 'SUNDAY' THEN
    pd_in_date := pd_in_date + 1;
    ln_days_left := ln_days_left - 1;
    WHEN pd_in_date = ld_next_holiday THEN
    pd_in_date := pd_in_date + 1;
    ln_days_left := ln_days_left - 1;
    ELSE
    ln_days_left := ln_days_left - 1;
    END CASE;
    END LOOP;
    RETURN(pd_in_date);
    END f_add_work_days;
    I think there is something wrong/missing in the logic as I can't get it to cater for say a double bank holiday(25/26th Dec - if the input parameters are 24/12/2007 and 2, which should return the 28th but returns the 26th!!).
    I'm relatively new to PL/SQL and Oracle, so any help, advice, ideas would be greatly appreciated!
    thanks in advance

    smth like
    SQL> with holidays as (select to_date('10.06.2007','dd.mm.yyyy') h_dt from dual),
      2       par as (select to_date('08.06.2007','dd.mm.yyyy') dt, 2 add_days from dual)
      3  --
      4  select min(dt) needed_date
      5    from (select p.*,
      6                 h.*,
      7                 mod(to_char(dt, 'j'), 7),
      8                 sum(decode(mod(to_char(dt, 'j'), 7), 5, 0, 6, 0, 1)+--get rid of sat and sun
      9                     nvl2(h_dt, -1, 0)--check if the day is holiday
    10                     ) over(order by dt) s
    11            from (select dt + level dt, add_days
    12                    from par
    13                  connect by level <= 100) p,
    14                 holidays h
    15           where h_dt(+) = dt
    16           order by 1)
    17   where add_days = s
    18  /
    NEEDED_DATE
    13.06.2007
    SQL> ?
    Message was edited by:
    Volder
    PS What Oracle version are you on?

  • Fucntion to Calculate business days (exclude weekends & holidays) between two days

    Hello,
    I need to be able to calculate business days between two dates excluding weekends and holidays. I do have a date dimension and also flags which denote weekday, holiday. Would really appreciate help in building a udf for the business days calculation.
    Thanks,

    Hi, Please take a look and tweak accordingly. Best of luck.
    --Assuming data dimension table something like the following
    --tblDateDim (DT date primary key,
    flagHoliday bit,
    flagWeekday bit
    CREATE FUNCTION fnBusinessDays (
    @StartDate DATE,
    @EndDate DATE
    RETURNS INT
    AS
    BEGIN
    DECLARE @Days INT ;
    SELECT @Days = count(*)
    FROM tblDateDim --// date dimension
    WHERE
    ( DT BETWEEN @StartDate AND @EndDate) --// dt is date column in the table.
    AND --// also flags which denote weekday, holiday
    flagHoliday = 0 --// flagHoliday= 1--Holiday;= 0 --workingday/businessday
    AND
    flagWeekday = 1 --// FlagWeekday = 1--businessday ; =0 --weekend (Saturday and Sunday)
    RETURN (@Days)
    END
    GO

  • ABAP HR- calculation of workind days (excluding weekend) for an employee

    Hi,
    I need to get number of working days between two dates excluding weekends for an employee. Please let me know below questions in this regard:
    1. I have to use Factory calender or holiday calender for this purpose?
    2. Will FM RKE_SELECT_FACTDAYS_FOR_PERIOD solve this purpose ?
    Regards,
    Aleria

    Hi Aleria,
    Use the FM DAY_ATTRIBUTES_GET in this FM pass the holiday and factory calendar of the employee along with the period. In the output structure if free day is set then it is SAT/SUN and if holiday is set then it is Public holiday marked in the calendar.
    Regards
    Bala

  • Exclude weekends when substracting between two date values

    Hi All,
    I created a variable to get the value between 2 date values.
    v_lag_days := TRUNC (l_header_rec.request_date) - TRUNC (SYSDATE);
    if l_header_rec.request_date = sysdate i.e. 17-Feb-12 then it is fine, I will get the value as '0' which is correct.
    But if l_header_rec.request_date = '20-Feb-12' (for example), it will give me as '3', but I should get the value as '1' excluding weekends.
    When ever with in the 2 date values if it contains weekends, it should exclude those weekend days and should give me the value in the variable.
    If you are not able to get my point clearly, please let me know.
    Could anyone please let me know the logic for my scenario. Please help me out that would be really appreciated.
    Thanks in advance,
    Satya.

    Hi Satya.
    This is the same example, without including the final day.
    WITH aux1 AS
         SELECT
              CASE WHEN TO_NUMBER(TO_CHAR(TO_DATE(:date1,'YYYYMMDD')+(LEVEL-1),'D')) >= 6
                   THEN 0
                   ELSE 1
              END inc
         FROM dual
         CONNECT BY LEVEL <= (TO_DATE(:date2,'YYYYMMDD') - TO_DATE(:date1,'YYYYMMDD'))
    SELECT SIGN(:date2 - :date1)*SUM(inc) num_of_days FROM aux1;
    -- ':date2 = 20120217'
    -- ':date1 = 20120210'
    NUM_OF_DAYS
              5
    -- ':date2 = 20120220'
    -- ':date1 = 20120217'
    NUM_OF_DAYS
              1
    -- ':date2 = 20120217'
    -- ':date1 = 20120217'
    NUM_OF_DAYS
              0Hope this helps.
    Best Regards
    Edited by: sKr on 17-feb-2012 9:04 Improved for date2 = date1

  • Calculating days excluding weekends

    Msy you please show me how to calculate the number of days in between 2 dates excluding weekends.

    Hi!
    Check out these FM-s:
    K_ABC_WORKDAYS_FOR_PERIODS_GET
    WEEK_GET_NR_OF_WORKDAYS      
    WLB3_GET_NUMBER_OF_WORKDAYS  
    There are some calendar relevant FM-s also. If there's set a production calendar in your SAP, you can check it also.
    SE37 transaction, and search for CALENDAR fm.
    Regards
    Tamá

  • Function module in SRM for adding number to date excluding weekends

    Hi Experts,
    Please help to get the standard FM in SRM for adding number to date and getting next date excluding weekends.
    Thanks in Advance.
    Thanks,
    Sahil

    This kind of custom FM can be created easily.
    But if you want to use standard FM then "DATE_CONVERT_TO_FACTORYDATE" FM is good but you need a factory calendar id which has all working days and weekend days specified (also public holidays if required). Then call this FM in loop for a count of days and it will give working day after "entered date + number of days"
    So if calendar has only weekdays and weekends specified, it will add number of days in specified date excluding weekends.
    Thanks,
    Murtuza

  • Time between dates excluding Weekends and Holidays

    I am fairly new to Power Pivot and I am having trouble with this formula.  I need my Cycle Time Days column to capture the days between CreatedDataTime and ClosedDateTime excluding Weekends and Holidays.  I have a tab that has the holidays but
    I can't get the formula correctly?   Jill
    CreatdDateTime        ClosedDateTime     Cycle Time Days
    12/1/2014                    12/10/2014

    first of all you need a separate time table which holds all possible dates for your model
    there should be no gaps, it should contain full years and a column of datatype DateTime as unique key
    once you have this in place add a calculated column to this table as 
    =IF(INT(FORMAT([Date], "w")) >= 6, "Weekend", "Workday")
    then go back to your original table and add the following calculation for your [Cycle Time Days]-column:
    =COUNTROWS(
    FILTER(
        'Dates',
        Dates[Date] >= DateRanges[DateFrom]
        && Dates[Date] <= DateRanges[DateTo]
        && 'Dates'[IsWeekend] = "Workday"
    Holidays are a bit more tricky but you can follow the same pattern but you need to get the holidays from somewhere
    hth,
    -gerhard
    Gerhard Brueckl
    blogging @ http://blog.gbrueckl.at
    working @ http://www.pmOne.com

  • DATEADD excluding weekends and holidays

    select getdate(),dateadd(day,15,Getdate())
    THis gets me today and 15 days from now. However I need 15 business days. I need to exclude weekends and holidays in here so that is looks like this. 
    where fulldate between getdate() and dateadd(day,15,Getdate())
    I have a dim table for dates and have an isholiday = 0 or 1. I just can't figure out how to include this in the DATEADD expression.

    Hi,
    try this link
    http://www.sqlservercentral.com/Forums/Topic1247790-391-1.aspx
    Hope will help you,
    Questo post è fornito &quot;così com'è&quot;. Non conferisce garanzie o diritti di alcun tipo. Ricorda di usare la funzione &quot;segna come risposta&quot; per i post che ti hanno aiutato a risolvere il problema e &quot;deseleziona
    come risposta&quot; quando le risposte segnate non sono effettivamente utili. Questo è particolarmente utile per altri utenti che leggono il thread, alla ricerca di soluzioni a problemi similari. ENG: This posting is provided &quot;AS IS&quot;
    with no warranties, and confers no rights. Please remember to click &quot;Mark as Answer&quot; on the post that helps you, and to click &quot;Unmark as Answer&quot; if a marked post does not actually answer your question. Please Vote This As
    Helpful if it helps to solve your issue. This can be beneficial to other community members reading the thread.

  • Working day formula that excludes weekends and holidays in WEBI

    Hi Guys,
    Can we calculate a working day formula that excludes weekends and holidays in WEBI OR in Universe?
    The universe I am working on is using stored procedures, so there are no joins or modelling done. Although there is a workday stored proc  that I can bring in the universe.
    I am just thinking if there is no modelling or joins done in the universe how will this help me in webi?
    Your suggestions will be very helpful.
    Thanks,
    Jitan

    One more suggestion I need from you  -
    I have a Work_Calendar_VW that has the following columns -
    Calendar_Date - 5/1/2014
    Calendar_Year - 2014
    Calendar_Month - 5
    Calendar_Day - 1
    Work_Day - 1
    Day_Type - WD (Work Day)  For Weekends this will be displayed as WE
    The columns have all dates for current year in SQL Server.
    I am going to pull this into the universe and create 2 derived tables to calculate MTD Day counts.
    Derived Table 1  - Calculate Yest Work Day... this will remove all the weekends and holidays and give me the last working day. This includes couple of case statments to check each day if it's a working day or weekend.
    For Work Day the above code will be 1 and for weekend 0
    Derived Table 2  - This will give me the count of working days in current month using the above view and Derived table 1 Below is the code  -
    SELECT COUNT(*)
    FROM Work_Calendar_VW
    WHERE [Work_Day] = 1 AND
       (Calendar_Date BETWEEN CAST(CONVERT(VARCHAR(25),MONTH(LastWoringkday()),101) + '/01/'
       + CONVERT(VARCHAR(25),YEAR(LastWoringkday()),101) AS DATE)
      AND LastWoringkday()).
    I cannot do joins because this universe has been built using stored proc and would like to implement this in BO universe.
    let me know if this is the right approach.
    Thanks,
    Jitan

  • Calcuate Hours: Minutes:Seconds between two dates excluding Weekends

    Hello All,
    @StartDate = '2014-06-21 13:37:30:037'
    @EndDate = GetDate()
    I want to find out Hours:Minutes:Seconds between the above dates EXCLUDING WEEKENDS.
    Please help.
    ReportingServices

    Hi,
    Give this a try:
    DECLARE @StartDate DATETIME = '2014-06-21 13:37:30:037'
    DECLARE @EndDate DATETIME = GETDATE()
    DECLARE @Base INT = 86400 -- ie Seconds in a days
    ;WITH cteCalculation AS (
    SELECT ((
    (DATEDIFF(dd, @StartDate, @EndDate) + 1)
    -(DATEDIFF(wk, @StartDate, @EndDate) * 2)
    -(CASE WHEN DATENAME(dw, @StartDate) = 'Sunday' THEN 1 ELSE 0 END)
    -(CASE WHEN DATENAME(dw, @EndDate) = 'Saturday' THEN 1 ELSE 0 END)
    ) * @Base)
    -(CASE WHEN DATENAME(dw, @StartDate) NOT IN ('Saturday', 'Sunday') THEN DATEPART(hh, @StartDate) * 3600 + DATEPART(mi, @StartDate) * 60 + DATEPART(ss, @StartDate) ELSE 0 END)
    -(CASE WHEN DATENAME(dw, @EndDate) NOT IN ('Saturday', 'Sunday') THEN @Base - (DATEPART(hh, @EndDate) * 3600 + DATEPART(mi, @EndDate) * 60 + DATEPART(ss, @EndDate)) ELSE 0 END)
    AS DiffInSec
    SELECT @StartDate
    ,@EndDate
    ,CAST(DiffInSec / 3600 AS VARCHAR(10)) + ':' +
    RIGHT('0' + CAST((DiffInSec % 3600) / 60 AS VARCHAR(2)), 2) + ':' +
    RIGHT('0' + CAST((DiffInSec % 3600 % 60) AS VARCHAR(2)), 2)
    FROM cteCalculation
    Feel free to modify to suit your needs!
    Hope this helps.
    ~J.

  • Configuring CUP SLA to exclude weekends

    Hi,
    Please advise if you know of where I can configure the CUP Service Level report to exclude weekends and public holidays (i.e. non-working days). Currently calls breach SLA sometimes on a non-working day.
    We are on GRC 5.3 SP13
    Thanks,
    Pumza

    I am pretty certain it can not read a holiday calendar, I dont think it is possible to ignore the weekends either.
    Regrads,
    Chinmaya

  • How to get difference in days between 2 dates excluding weekends

    Hi all,
    i have a requirement, to calculate the difference between 2 dates, in days.
    eg: 01/08/2007 and 05/08/2007.
         Difference = 4 days.
    But here my actual requirement is i have to calculate this difference excluding weekends (saturday n sundays).
    eg: 01/08/2007 -
    Thursday
         05/08/2007 -
    Monday
    so now Difference = 2 days.
    Please help me regarding this.
    Points will be rewarded for helpfull answers.
    Thanks in Advance.
    Regards,
    Vineel

    see these codes of rich
    report zrich_0003.
    data: begin of itab occurs 0,
          datum type sy-datum,
          end of itab.
    data: weekday like dtresr-weekday.
    data: number_lines type i.
    parameters: p_sdatum type sy-datum,
                p_edatum type sy-datum.
    itab-datum = p_sdatum.
    append itab.
    do.
      if itab-datum = p_edatum.
        exit.
      endif.
      itab-datum = itab-datum + 1.
      call function 'DATE_TO_DAY'
           exporting
                date    = itab-datum
           importing
                weekday = weekday.
      if weekday = 'Sat.'
        or weekday = 'Sunday'.
        continue.
      endif.
      append itab.
    enddo.
    describe table itab lines number_lines.
    write:/ 'Number of days between dates is', number_lines.
    and
    report zrich_0001.
    parameters: p_start type sy-datum,
                p_end   type sy-datum.
    data: idays type table of   rke_dat with header line.
    data: workingdays type i.
    call function 'RKE_SELECT_FACTDAYS_FOR_PERIOD'
         exporting
              i_datab               = p_start
              i_datbi               = p_end
              i_factid              = 'P8'  " Fact Calender ID
         tables
              eth_dats              = idays
         exceptions
              date_conversion_error = 1
              others                = 2.
    describe table idays lines workingdays.
    write:/ workingdays.
    I want to find the No.of working days between the two dates
    regards,
    srinivas

Maybe you are looking for