Search code examples
sql-servert-sqlstored-functions

User defined function sql, Incorrect syntax near the keyword 'return'


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


Solution

  • 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