0

I have this query developed with the help of a guy here and I am not able to use this in function. There is some sort of Syntax issue.

Here is the query

WITH CTE AS (
    SELECT @STARTDATE AS STARTDATE
    UNION ALL
    SELECT DATEADD(D,1,STARTDATE) 
    FROM CTE
    WHERE STARTDATE <@ENDDATE
),
WORKINGDAYS AS (
    SELECT STARTDATE,
           DATENAME(DW,STARTDATE)WEEKDAYS,
           C1.CalanderDayName AS isweekend
    FROM CTE c
         LEFT JOIN HRM.tbl_Calendar C1 ON DATENAME(DW,STARTDATE) = C1.CalanderDayName
                                      AND C1.IsOffDay = 1
)

SELECT COUNT(WEEKDAYS)as WORKINGDAYS
FROM WORKINGDAYS
WHERE isweekend IS NULL;

I want to create a function named fnGetWorkingDays

ALTER FUNCTION [dbo].[fnGetWorkingDays] (@StartDate datetime, @EndDate datetime)
RETURNS int
AS
BEGIN
     DECLARE @dateFrom datetime 
     DECLARE @dateTo datetime 
     SET @dateFrom = @StartDate 
     SET @dateTo = @EndDate

     DECLARE @WORKDAYS INT
     SELECT @WORKDAYS =

;WITH CTE AS (
SELECT @STARTDATE  AS STARTDATE
UNION ALL
select DATEADD(D,1,STARTDATE) 
FROM CTE
WHERE STARTDATE <@ENDDATE
)
,WORKINGDAYS AS (
SELECT STARTDATE,DATENAME(DW,STARTDATE)WEEKDAYS, C1.CalanderDayName AS isweekend
FROM CTE c
LEFT JOIN HRM.tbl_Calendar C1 ON DATENAME(DW,STARTDATE)=C1.CalanderDayName AND C1.IsOffDay=1
)

SELECT COUNT(WEEKDAYS)as WORKINGDAYS FROM WORKINGDAYS WHERE isweekend is null

     RETURN @WORKDAYS
END
Doonie Darkoo
  • 1,475
  • 2
  • 14
  • 33
  • It's probably the ; at the start; but please post the full create function code & the error you get, thanks. – Gareth Lyons Feb 13 '18 at 12:48
  • "Some sort of Syntax issue."? Could you elaborate? Are you getting an error? If so, what so? – Thom A Feb 13 '18 at 12:48
  • @Larnu Yes I'm editing a question and exactly I'm getting semicolon error – Doonie Darkoo Feb 13 '18 at 12:52
  • "a semicolon error"? You haven't put the error in your post. Please put it in. – Thom A Feb 13 '18 at 12:55
  • This now looks like you're trying to reinvent the wheel. There are a lot of examples out there on how to get the number of working days between 2 dates, and the good one's use a inline table-value function, not a scalar function (which perform poorly). – Thom A Feb 13 '18 at 13:00

3 Answers3

3

Try With this Below function .let me know back for any errors

CREATE FUNCTION [dbo].[fnGetWorkingDays] (@StartDate datetime, @EndDate datetime)
RETURNS int
AS
BEGIN 

     DECLARE @WORKDAYS INT


;WITH CTE AS (
    SELECT @STARTDATE AS STARTDATE
    UNION ALL
    SELECT DATEADD(D,1,STARTDATE) 
    FROM CTE
    WHERE STARTDATE <@ENDDATE
),
WORKINGDAYS AS (
    SELECT STARTDATE,
           DATENAME(DW,STARTDATE)WEEKDAYS,
           C1.CalanderDayName AS isweekend
    FROM CTE c
         LEFT JOIN HRM.tbl_Calendar C1 ON DATENAME(DW,STARTDATE) = C1.CalanderDayName
                                      AND C1.IsOffDay = 1
)

SELECT @WORKDAYS=COUNT(WEEKDAYS) FROM WORKINGDAYS WHERE isweekend is null

     RETURN @WORKDAYS
END
Mahesh.K
  • 901
  • 6
  • 15
  • what is the diff between my answer and your one? – Sandip - Frontend Developer Feb 13 '18 at 13:01
  • Yes it worked too. I got the idea now where I was going wrong. Thank you again – Doonie Darkoo Feb 13 '18 at 13:05
  • why down vote .i tried the query my own way without seeing yours or copy/pasting it back . As OP mentioned that `query developed with the help of a guy` is developed by me For your information check https://stackoverflow.com/a/48765379/6122941 .Syntax of creating a function will be same everywhere .Won't varies user to user right . So obviously it looks similar .just understand that .Blindly make downvotes doesn't makes any sense .Hope you understand .Thanks – Mahesh.K Feb 13 '18 at 13:07
  • @DoonieDarkoo If you reached your expectation accept the answer reward back us . Great to help you . Happy Coding -:) – Mahesh.K Feb 13 '18 at 13:09
  • Yes I did and thank you. Also I have commented on previous post please check that too – Doonie Darkoo Feb 13 '18 at 13:10
  • @SandipPatel In my function i didn't declared the same variables again .Just compare the code clearly . – Mahesh.K Feb 13 '18 at 13:10
1

You are getting the Error because your Assignment operation for the Variable @WORKDAYS is Wrong. Change it Like This

ALTER FUNCTION [dbo].[fnGetWorkingDays]
(
    @StartDate DATETIME,
    @EndDate DATETIME
)
RETURNS INT
AS
BEGIN
    DECLARE @dateFrom DATETIME;
    DECLARE @dateTo DATETIME;
    SET @dateFrom = @StartDate;
    SET @dateTo = @EndDate;
    DECLARE @WORKDAYS INT;

    WITH CTE
    AS 
    (
       SELECT 
          @STARTDATE AS STARTDATE
       UNION ALL
       SELECT 
          DATEADD(D, 1, STARTDATE)
          FROM CTE
             WHERE STARTDATE < @ENDDATE
    ),WORKINGDAYS
    AS 
    (
       SELECT
          DATENAME(DW, STARTDATE) WEEKDAYS,
          C1.CalanderDayName AS isweekend
          FROM CTE c
             LEFT JOIN HRM.tbl_Calendar C1 
                ON DATENAME(DW, STARTDATE) = C1.CalanderDayName
                    AND C1.IsOffDay = 1
    )
    SELECT 
       @WORKDAYS = COUNT(WEEKDAYS)--Asign Variable Here
       FROM WORKINGDAYS
          WHERE isweekend IS NULL;

    RETURN @WORKDAYS;

END;
Jayasurya Satheesh
  • 7,826
  • 3
  • 22
  • 39
0

You have written wrong syntax to set @WORKDAYS, try below syntax

CREATE FUNCTION [dbo].[fnGetWorkingDays] (@StartDate datetime, @EndDate datetime)
RETURNS int
AS
BEGIN        

    DECLARE @WORKDAYS INT   

    ;WITH CTE AS (
    SELECT @STARTDATE  AS STARTDATE
    UNION ALL
    select DATEADD(D,1,STARTDATE) 
    FROM CTE
    WHERE STARTDATE <@ENDDATE
    )
    ,WORKINGDAYS AS (
    SELECT STARTDATE,DATENAME(DW,STARTDATE)WEEKDAYS, C1.CalanderDayName AS isweekend
    FROM CTE c
    LEFT JOIN HRM.tbl_Calendar C1 ON DATENAME(DW,STARTDATE)=C1.CalanderDayName AND C1.IsOffDay=1
    )

    SELECT @WORKDAYS=COUNT(WEEKDAYS) FROM WORKINGDAYS WHERE isweekend is null

     RETURN @WORKDAYS
END
  • 1
    Semicolons are Terminators, not "beginninators". you should be ending **every** line with a semicolon, `;`, not starting the expressions that require the previous statement to be **terminated** with one. – Thom A Feb 13 '18 at 12:55
  • Yes I am aware with that but I guess CTE expression requires previous expression to be terminated which I guess I have implemented in pretty much messy way. Thanks for the help though – Doonie Darkoo Feb 13 '18 at 13:04