2

I'm trying to call a function from itself but it gives me the error

'CalculateHoursBetweenDates' is not a recognized built-in function name.

I know this is because I haven't created the function but I am calling it from inside the function itself.

This is my code:

create function CalculateHoursBetweenDates
     (@start varchar(50), 
      @end varchar(50), 
      @days int = 0) 
returns int
as 
begin
    declare @dayDiff int = DATEDIFF(d, @start, @end) + DATEPART(dw, @start)

    if(@dayDiff > 6) 
    begin
        set @days = @days + (DATEPART(dw, @start) -7)
        set @start = DATEADD(d, 7 - (datepart(dw, @start) - 2), @start)
        return CalculateHoursBetweenDates(@start, @end, @days)
    end 

    return @days + DATEDIFF(d, @start, @end)
end

Am I calling it the wrong way or what? Do I have to create an empty function first? I don't get it

3
  • 3
    Try prefixing it with dbo.: dbo.CalculateHoursBetweenDates Commented Jun 11, 2018 at 20:05
  • ok, that worked. Can you explain me why it needs the schema to be specified for this? Commented Jun 11, 2018 at 20:06
  • 1
    @TylerRoper whoops indeed, it matters. Retracted my vote to close. Commented Jun 11, 2018 at 20:07

1 Answer 1

3

I suspect you need schema prefix :

select <scheme>.CalculateHoursBetweenDates (. . . .)

However, the default schema name is dbo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.