1
CREATE FUNCTION weekdays (DATE1 DATETIME, DATE2 DATETIME) 
    RETURNS INTEGER
    BEGIN 
        RETURN DATEDIFF(day, DATE1, DATE2);
    END

ERROR: syntax error at or near "BEGIN" LINE 3: BEGIN

What am I missing. I've tried a number of variations but nothing has solved it yet. I'm using MySQL.

2
  • 1
    DATEDIFF takes two arguments. You have 3 in here. Commented Jan 1, 2017 at 17:18
  • What are you actually trying to do? Commented Jan 1, 2017 at 17:34

2 Answers 2

1

Try this:

delimiter $$

CREATE FUNCTION weekdays (DATE1 DATETIME, DATE2 DATETIME) 
    RETURNS INTEGER
    BEGIN 
        RETURN DATEDIFF(DATE1, DATE2);
    END $$

DATEDIFF take only two params.

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

Comments

0

How it worked for me:

SELECT A.DOCNUMBR,A.DOCDATE,DATEDIFF(DAY,A.DOCDATE,GETDATE()) AS DAYS_CALC,
  FROM RM_TRANSACTIONS AS A
WHERE A.CURTRXAMNT <> 0

The result presents the Document Number, the Document Date and the age of the Document in number of days.

This is very useful if you want to eventually create an Aging Report later in Excel.

Erwin-PR

1 Comment

The Question is asking for a function, not a SQL

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.