Hi
Can someone help me here, I am not a DB guy, I am BA and need some assistance for data mining.
I have a function- fn_nonworkingdays (input- start date, calendar_id) returns int (0-working day, 1-nonworking day).
Now I need to create another function that actually checks if
1. it takes the input date and calculates the month_end_date. (eg input date-9/10/2014, then month_end_date=9/30/2014)
2. Now it should validate the month_end_date from this with the above function (fn_nonworkingdays) to check if 9/30/2014 is a working or nonworking day,
a. if working calculate no of days in that month.
b. if not working day, then step over to next day i.e. 10/1/2014 and check if that is a working day, it needs to check in the above function only. if it is working then do same as #a, if not repeat step#b until the working day is reached.
Note the calendar is not specific to USA, I pass calendar_ID which is particular to a country.
This is what I created-
alter FUNCTION dbo.fn_MTD_V1
(
@REPORT_DATE VARCHAR (20) ,
@calendar_id smallINT,
)
RETURNS INT
AS
BEGIN
DECLARE @STARTDATE AS DATETIME ;
DECLARE @REPORTDATE AS DATETIME ;
DECLARE @DayAfterReportDate AS DATETIME ;
DECLARE @TOTALDAYCOUNT INT
DECLARE @ENDDATE as DATETIME;
SELECT @REPORTDATE=@REPORT_DATE
SELECT @StartDate=CONVERT(DATETIME,DATEADD(dd,-(DAY(@REPORTDATE)-1),@REPORTDATE),111)
SELECT @ENDDATE=DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@REPORTDATE)+1,0))
SELECT @DayAfterReportDate=DATEADD(day, 1, @ENDDATE)
IF (dbo.ABfn_IsNonWorkingDay(@ENDDATE, @calendar_id)=0) --0=working and 1=non-working
BEGIN
SELECT @TotalDayCount= DATEDIFF(DAY, @StartDate, @ENDDATE)
END
-----------------------NEXT DAY-----------------------------------------------------------------------------------------
ELSE IF (dbo.ABfn_IsNonWorkingDay(@REPORTDATE, @calendar_id)=1 AND (dbo.ABfn_IsNonWorkingDay(@DayAfterReportDate, @calendar_id)=0))
BEGIN
SELECT @DayAfterReportDate=@DayAfterReportDate+1
SELECT @TotalDayCount= DATEDIFF(DAY, @StartDate, @DayAfterReportDate)
END
-----------------------DAY AFTER NEXT DAY-------------------------------------------------------------------------------
ELSE IF (dbo.ABfn_IsNonWorkingDay(@REPORTDATE, @calendar_id)=1 AND (dbo.ABfn_IsNonWorkingDay(@DayAfterReportDate, @calendar_id)=1))
BEGIN
SELECT @DayAfterReportDate=@DayAfterReportDate+2
SELECT @TotalDayCount= DATEDIFF(DAY, @StartDate, @DayAfterReportDate)
END
Return @TotalDayCount
I am not sure how to put the above case in loop, because sometimes there can be several days which can be holidays at a stretch. Say an entire week can be holiday, in which case I need some kind of looping query.
Please help.
Can someone help me here, I am not a DB guy, I am BA and need some assistance for data mining.
I have a function- fn_nonworkingdays (input- start date, calendar_id) returns int (0-working day, 1-nonworking day).
Now I need to create another function that actually checks if
1. it takes the input date and calculates the month_end_date. (eg input date-9/10/2014, then month_end_date=9/30/2014)
2. Now it should validate the month_end_date from this with the above function (fn_nonworkingdays) to check if 9/30/2014 is a working or nonworking day,
a. if working calculate no of days in that month.
b. if not working day, then step over to next day i.e. 10/1/2014 and check if that is a working day, it needs to check in the above function only. if it is working then do same as #a, if not repeat step#b until the working day is reached.
Note the calendar is not specific to USA, I pass calendar_ID which is particular to a country.
This is what I created-
alter FUNCTION dbo.fn_MTD_V1
(
@REPORT_DATE VARCHAR (20) ,
@calendar_id smallINT,
)
RETURNS INT
AS
BEGIN
DECLARE @STARTDATE AS DATETIME ;
DECLARE @REPORTDATE AS DATETIME ;
DECLARE @DayAfterReportDate AS DATETIME ;
DECLARE @TOTALDAYCOUNT INT
DECLARE @ENDDATE as DATETIME;
SELECT @REPORTDATE=@REPORT_DATE
SELECT @StartDate=CONVERT(DATETIME,DATEADD(dd,-(DAY(@REPORTDATE)-1),@REPORTDATE),111)
SELECT @ENDDATE=DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@REPORTDATE)+1,0))
SELECT @DayAfterReportDate=DATEADD(day, 1, @ENDDATE)
IF (dbo.ABfn_IsNonWorkingDay(@ENDDATE, @calendar_id)=0) --0=working and 1=non-working
BEGIN
SELECT @TotalDayCount= DATEDIFF(DAY, @StartDate, @ENDDATE)
END
-----------------------NEXT DAY-----------------------------------------------------------------------------------------
ELSE IF (dbo.ABfn_IsNonWorkingDay(@REPORTDATE, @calendar_id)=1 AND (dbo.ABfn_IsNonWorkingDay(@DayAfterReportDate, @calendar_id)=0))
BEGIN
SELECT @DayAfterReportDate=@DayAfterReportDate+1
SELECT @TotalDayCount= DATEDIFF(DAY, @StartDate, @DayAfterReportDate)
END
-----------------------DAY AFTER NEXT DAY-------------------------------------------------------------------------------
ELSE IF (dbo.ABfn_IsNonWorkingDay(@REPORTDATE, @calendar_id)=1 AND (dbo.ABfn_IsNonWorkingDay(@DayAfterReportDate, @calendar_id)=1))
BEGIN
SELECT @DayAfterReportDate=@DayAfterReportDate+2
SELECT @TotalDayCount= DATEDIFF(DAY, @StartDate, @DayAfterReportDate)
END
Return @TotalDayCount
I am not sure how to put the above case in loop, because sometimes there can be several days which can be holidays at a stretch. Say an entire week can be holiday, in which case I need some kind of looping query.
Please help.