4

I'm trying to write a function that takes an input of a date, and calculates the difference between current day and the inputed date, then returns it as an integer of days. (So if I input yesterday 2015-5-8 it returns 1). So far I'm getting this error and can't really figure out what's wrong. Any help is appreciated.

CREATE FUNCTION DnevnaRazlika
(@OdKdaj nvarchar(15))
RETURNS INT 
AS
BEGIN 
declare @return INT
select @return = DATEDIFF(day,@OdKdaj, CONVERT(date,GETDATE()))
end
return @return 
end

Edit: Using Microsoft SQL management studio

3 Answers 3

3

Too much ends. try below code

CREATE FUNCTION DnevnaRazlika ( @OdKdaj NVARCHAR(15) )
RETURNS INT
AS 
    BEGIN 
        DECLARE @return INT
        SELECT  @return = DATEDIFF(day, @OdKdaj, CONVERT(DATE, GETDATE()))
        RETURN @return 
    END
Sign up to request clarification or add additional context in comments.

1 Comment

This answer was helpful because it told me that the issue truly was syntax (Although mine for a different reason.). Thank you
1

problem here is you are having end two time

CREATE FUNCTION DnevnaRazlika
(@OdKdaj nvarchar(15))
RETURNS INT 
AS
BEGIN 
declare @return INT
select @return = DATEDIFF(day,@OdKdaj, CONVERT(date,GETDATE()))
--end remove this end than it will work 
return @return 
end

Comments

0
create Function a123(@a Int,@b Int) returns Int as 
begin 
    return (@a + @b)
end

select dbo.a123(30,20)

1 Comment

Answers should also include a small explanation, even in the case of very simple ones.

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.