Udf FMS date update excluding Weekend
Hello friends,
Have made a udf in the Sales Order Header as Final Approval Drawing Req Date U_FAADT in which I want that if a Sales Order posting date is 14/12/2010 then this udf should automatically update with the next day date i.e 15/12/2010 but if the next day is Friday or Saturday then the date should come for Sunday.
The requirement is as such that Friday and Saturday are holidays for the company and the client wants to have this field updated on 24 hours basis but it should not consider Friday and Saturday as its holiday. Also note in SAP base setup Holiday Master Weekend is set from Friday to Saturday. Will this help in any way of updating the field or through formated search.
Kindly advise what best solution can be given
Regards,
Soni
Hi Soli,
Try this:
SELECT
CASE WHEN DatePart(DW,$[ORDR.DocDate.Date])=5
THEN DateAdd(DD, 3, $[ORDR.DocDate.Date])
WHEN DatePart(DW,$[ORDR.DocDate.Date])=6
THEN DateAdd(DD, 2, $[ORDR.DocDate.Date])
ELSE
DateAdd(DD, 1, $[ORDR.DocDate.Date])
END
Thanks,
Gordon
Similar Messages
-
Hello -
I've been tearing my hair out over this problem i'm trying to solve, probably just been staring at it too long which is making it worse -
I have a series of open support tickets which are supposed to be updated on a daily basis, the problem is that they aren't always being updated daily. So, the business wants to know the number of days from when a ticket was last updated and today's
date. I have this basic calculation and it's working fine, however now the business wants to exclude weekends from the calculation. The other problem is that some reps DO go in on weekends and update their tickets, so sometimes there will be updates
made on weekend dates.
To give an example -
Today's date is 2014-02-10 (Monday). A ticket was last updated last Thursday, 2014-01-30. The difference between the two dates is 11, so it's been 11 days since the ticket was last updated. Now, if I exclude Saturdays and Sundays, then
it's actually been 7 days since the ticket was last updated. I'm not sure how to do this in T-SQL.
Now, to further complicate the problem, sometimes a ticket IS updated on a Saturday or Sunday. So, if a ticket was updated on 2014-02-02 (Sunday), then it should be counted. Again i'm not sure how to do this.
What gets me is that this is probably fairly simple and i've just been staring at it too long. In the meantime, can someone offer some guidance?
Thanks!!I've adapted this from a function on my blog. you will need to add set the YourTicketTable to where ever your tickets are stored.
CREATE
FUNCTION [dbo].[CalcWorkDaysBetween](@StartDate
As DateTime,@EndDate
AS DateTime)
RETURNS
INT AS
BEGIN
SET @EndDate
=DATEADD(DAY,1,@EndDate)
DECLARE @Count
AS Int= 0
DECLARE @Date
As Date=@StartDate
WHILE @Date
< @EndDate
BEGIN
IF (DATEPART(WEEKDAY,@Date)IN(1,7)
OR (SELECT
Count(*)
FROM YourTicketTable WHERE TicketDate=@Date)=1)
BEGIN
SELECT @Count = @Count
+ 1
END
SELECT @Date=DATEADD(Day,
1,@Date)
END
RETURN
DATEDIFF(DAY,@StartDate,@EndDate)- @Count
END
Regards, -
Hi
I am creating a simple design studion project request form where the user can enter how many photographs need to be ordered. I am calling this field 'NumImages'. Then once the data is entered, another field (I am calling this NumDaysImage) will be calculated based on the fact that no more than 5 photographs can be shot per day. So, 1-5 photos = 1 day. 6-10 photos = 2 days. 11-15 photos = 3 days etc. I do not need to calculate part days, so 6 photos is the same as 10 i.e. 2 days.
Once I have calcuated 'NumDaysImage', I then need to calculate an end date called 'DateDue'. The start date is entered by the user in the field 'DateStart'.
I know this is not going to be easy, but how can I calculate 'DateDue' based on a Mon-Fri working week, and ideally excluding UK public holidays?
Also on the form is a value 'NumDays' that is the number of days for designing the brochure for the photos. This value I have calculated already, but it will need to be added to 'NumDaysImage' to get a total number of production days that is then used to calculate 'DateDue'.
Any help will be greatly appreciated. Thanks.JavaScript has the Math.ceil method which rounds up a number to the next integer if it is not an integer.
Dates are more complicated calculation as the starting date needs to be converted to a number and then one can calculate the ending date and test that date to see if is a Sunday or Saturday. Do intervening weekends add to the number of days for processing. -
Date difference function that returns minutes between two dates and excludes weekends and holidays
Is there a way to get this to work as a function?
Currently returns error "Select statements included within a function cannot return data to a client"
CREATE FUNCTION [dbo].[WorkDays](@DateFrom datetime,@DateTo datetime)
RETURNS int
AS
BEGIN
--Working time
DECLARE @WTFrom TIME = '8:00AM';
DECLARE @WTTo TIME = '5:00PM';
DECLARE @minCount BIGINT
--Date ranges
IF (DATEDIFF(HOUR, @DateFrom, @DateTo) > 12)
BEGIN
WITH CTE AS
SELECT @DateFrom AS DateVal
UNION ALL
SELECT DATEADD(HOUR, 1, DateVal)
FROM CTE
WHERE DateVal < DATEADD(HOUR, -1,@DateTo)
SELECT DATEDIFF(minute, MIN(CTE.DateVal), MAX(CTE.DateVal))
FROM CTE
WHERE (CAST(CTE.DateVal AS time) > @WTFrom AND CAST(CTE.DateVal AS time) < @WTTo) AND DATEPART(dw, CTE.DateVal) NOT IN (1, 7) AND NOT EXISTS (SELECT * FROM Holiday AS H WHERE H.holiday = CTE.DateVal)
OPTION (MAXRECURSION 0);
END;
ELSE
BEGIN
WITH CTE AS
SELECT @DateFrom AS DateVal
UNION ALL
SELECT DATEADD(MINUTE, 1, DateVal)
FROM CTE
WHERE DateVal < DATEADD(MINUTE, -1,@DateTo)
SELECT DATEDIFF(minute, MIN(CTE.DateVal), MAX(CTE.DateVal))
FROM CTE
WHERE (CAST(CTE.DateVal AS time) > @WTFrom AND CAST(CTE.DateVal AS time) < @WTTo) AND DATEPART(dw, CTE.DateVal) NOT IN (1, 7) AND NOT EXISTS (SELECT * FROM Holiday AS H WHERE H.holiday = CTE.DateVal)
OPTION (MAXRECURSION 0);
END;
END
Thanks for your help.Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Learn how to follow ISO-11179 data element naming conventions and formatting rules (you do not). Temporal
data should use ISO-8601 formats (you do not!). Code should be in Standard SQL as much as possible and not local dialect.
This is minimal polite behavior on SQL forums.
We hate functions in SQL. This is a declarative language and you are using it like 1950's FORTRAN. We hate local variables (more FORTRAN!)
The name of a function has to be either a known, common name, like “sine”, “cosine” etc. Or it is “<verb>_<object>”; you think a noun is a verb! Do you really need BIGINT? Why did you invite garbage data with it? Why do you think that SQL
uses AM/PM? Have you never seen the TIME data type?
Think about “date_val” as a data element name. A date is a unit of temporal measurement on a calendar scale. This would be a “<something>_date” in a valid schema.
>> Is there a way to get this to work as a function? <<
Probably, but why do it wrong?
Build a calendar table with one column for the calendar data and other columns to show whatever your business needs in the way of temporal information. Do not try to calculate holidays in SQL -- Easter alone requires too much math.
The julian_business_nbr is how SQL people do this. Here is the skeleton.
CREATE TABLE Calendar
(cal_date DATE NOT NULL PRIMARY KEY,
julian_business_nbr INTEGER NOT NULL,
Here is how it works:
INSERT INTO Calendar
VALUES ('2007-04-05', 42),
('2007-04-06', 43), -- Good Friday
('2007-04-07', 43),
('2007-04-08', 43), -- Easter Sunday
('2007-04-09', 44),
('2007-04-10', 45); --Tuesday
To compute the business days from Thursday of this sample week to next Tuesday:
SELECT (C2.julian_business_nbr - C1.julian_business_nbr)
FROM Calendar AS C1, Calendar AS C2
WHERE C1.cal_date = '2007-04-05',
AND C2.cal_date = '2007-04-10';
See how simple it can be when you stop trying to write FORTRAN and think in sets?
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
FMS for Udf based on Posting Date + 4 weeks and +4 weeks excluding weekends
Hello,
Requirement 1
Have made a udf in the Sales Order Header as Manfacture Drawing Rel Date U_FDRDT in which I want that if a Sales Order posting date is 15/12/2010 then this udf should automatically update with the + 4 weeks date i.e 14/01/2011
Requirement 2
Another UDF is also made as U_FRDWDT in which I want that if the Sales Order posting date is 15/12/2010 then this udf should automatically update + 4 weeks EXCLUDING Fridays and Saturdays which is there in the 4 weeks hence the result should be e.g 27/01/2010 which is excluding Fridays and Saturdays.The requirement is as such that Friday and Saturday are holidays for the company and the client wants to have this field updated on + 4 weeks basis but it should not consider Friday and Saturday as its holiday.
Also note in SAP base setup Holiday Master Weekend is set from Friday to Saturday. Will this help in any way of updating the field or through formated search.
Kindly advise what best solution can be given
Regards,
SoniHi,
One question per thread. This is forum rule.
Here is FMS for the 1st one:
SELECT DateAdd(WW,4,$[ORDR.DocDate\])
Please post another for the 2nd.
Thanks,
Gordon -
FMS required for Posting Date + 4 week excluding weekends
Hi,
I have a UDFas U_FRDWDT in which I want that if the Sales Order posting date is 15/12/2010 then this udf should automatically update + 4 weeks EXCLUDING Fridays and Saturdays which is there in the 4 weeks hence the result should be e.g 27/01/2010 which is excluding Fridays and Saturdays.The requirement is as such that Friday and Saturday are holidays for the company and the client wants to have this field updated on + 4 weeks basis but it should not consider Friday and Saturday as its holiday.
Also note in SAP base setup Holiday Master Weekend is set from Friday to Saturday. Will this help in any way of updating the field or through formated search.
Kindly advise what best solution can be given
Regards,
SoniHi Gordon,
I used this formatted search and seeing some discripencies as if lets asume the Sales Order Posting Date is 15/12/2010 then 4 weeks should come in the udf means excluding friday and saturday and the date which is coming as per fms is 12/01/2011 but i guess the fms should show 25/01/2010 which is 4 weeks ( no of days 30 ) excluding friday and saturday. I think your fms is just brining 22 working days and then its showing 12/01/2010.
I want the fms to consider friday and saturday as holiday and then give me 4 weeks.
I hope i am clear to my requirement and able to make you understand.
Regards,
Soni -
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 advancesmth 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? -
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 -
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,
SahilThis 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/2014first 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 -
Calculate the Difference Between two dates excluding weekends and Holidays
Hi,
We need to calculate the difference between the two dates by excluding the Local public holidays (It is global and varies across countries) and weekends should not be included in calculation for a business day in OBIEE.
We have two dates: Open date and close date when ever close date is null we are calculating age based on taking the current timestamp and need to exclude the weekends and Holidays when ever the close date is null.
Ex:
Col1 col2 Total
11/9/2010 2:46:38 PM Null 13
11/2/2010 8:06:26 PM 11/3/2010 5:37:03 PM 1
(In the Total we shouldn't include the weekends,holidays)
Please let me know how to calculate the difference between two dates by excluding the weekends and holidays.
Thanks
Edited by: user10441472 on Nov 22, 2010 3:14 PMYou already asked this question and I answered it...
Re: calculation of Business day in OBIEE -
Req. deliv.date should exclude holidays and weekends
Hi guy,
When I try to create my sales order. The Req.deliv.date and First date will automatically fill with date. The question is it should exclude weekends and holiday, but the result is not. Just like that
The 07/12/2014 is Saturday. How should I make it automatically exclude weekends? Thank you in advanced.
Best,
PeterHi Peter,
The requested delivery date is the date that the customer is requesting the material arrive at their site. Therefore, the logical calendar to check this against is the unloading point calendar of the customer. Please maintain an unloading point and assign a calendar for the customer in transaction VD02.
When you do this, the system will issue a warning of you enter a non-working day. There is not possibility to change this to an error in the standard system. The system assumes that the user knows what they are doing and takes priority over the warning message. However, the warning message does inform the user of the next working day.
I remember replying to a similar thread about this before. You can find it here:
No Schedule Line Dates on Weekends -
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.
ReportingServicesHi,
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. -
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,
Vineelsee 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 -
How to exclude weekends & holidays from a date range
Hi Friends,
How can I exclude weekends & other holidays (which are specific to that country only) from a given range of date (if any weenends or holiday falls in that range)?
Please supply some sample code if possible.
Thanks in advance.Enayats_screen wrote:
Hi
I have found the relevant code for your help. May be this will solve your purpose. Please visit the below link
http://www.simplyshareit.net/itemDetails.jsf?q=532&
Thanks.Whew! Thank the gods you saved the OP from having to actually think or learn or clearly communicate his problem!
Maybe you are looking for
-
Linux clearing of screen after command (man, vi, etc.) termination
One small nuisance in perhaps every Linux distro is the clearing of the screen after the execution of commands like man, vim or git. I find this irritating because I might be very interested in seeing the output of say, 'git log' and comparing it wit
-
The app store interface is maddening. if i enter my password wrong once i have to reset it?! much fail. all im trying to do is download a free program, why do i have to enter payment information? i dont mind. but why cant i just skip it. so when i ac
-
VBA help file in Word 2007 won't open
When trying to open the VBA help in Word 2007 the following message occurs:"This webpage might not be displayed properly. The content of the webpage does not match the type specified by the server. Click here for options..." After clicking, I get
-
i cannot hear any sound from music player and also i am unable to listen to any calls i am receiving.kindly help.
-
After I finish a search, How do I close the search window?
after I finish a search, how do i close the search window?