1

I'm getting the following error whenever i'm trying to create the following function in mysql. error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4

And my function is

    CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
    RETURNS VARCHAR(16)
    BEGIN
    DECLARE result VARCHAR(16);
    IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
    ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
    RETURN result;
    END
0

1 Answer 1

1

You need to change the delimiter before creating a function. If you don't your CREATE statement terminates at the first semi-colon.

Try this:

DELIMITER $$

CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
    RETURNS VARCHAR(16)
    BEGIN
    DECLARE result VARCHAR(16);
    IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
    ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
    END IF
    RETURN result;
    END$$

DELIMITER ;

Thanks to @Ravinder for catching the missing END IF.

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

1 Comment

Add END IF before RETURN statement. And it should be working.

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.