(see my answer below for solution – thanks for the feedback)
It’s probably something really obvious but I can’t see what’s wrong with my sql:
mysql> CREATE FUNCTION start_of_minute(
-> curdate DATE)
-> RETURNS DATE
-> DETERMINISTIC
-> SQL SECURITY INVOKER
-> BEGIN
-> DECLARE sofm DATE;
-> SET sofm = SUBDATE (
-> curdate,
-> INTERVAL SECOND(curdate) SECOND
-> );
-> RETURN sofm;
-> END //
ERROR 1064 (42000): 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 ');
RETURN sofm;
END' at line 11
All the more frustrating as the following works:
mysql> CREATE FUNCTION start_of_week(
-> curdate DATE,
-> first_day_of_week INTEGER)
-> RETURNS DATE
-> DETERMINISTIC
-> SQL SECURITY INVOKER
-> BEGIN
-> DECLARE sow DATE;
-> SET sow = SUBDATE(
-> curdate,
-> INTERVAL (WEEKDAY(curdate)+(7-first_day_of_week)%7) DAY
-> );
-> RETURN sow;
-> END //
Query OK, 0 rows affected (0.00 sec)
(NB there may be other ways of truncating the date at various levels – and I’d certainly be interested in hearing about them, I really want to know what’s wrong with my syntax – not a different method for calculating the start of a period).
Yes, second() is a valid function, and SECOND is a valid interval.
TIA
I had already considered that it was getting upset about the types – however replacing all the ‘date’ types with ‘DATETIME’ types did not resolve the problem – turns out the problem was a space between ‘SUBDATE’ and ‘(‘ – I never knew MySQL was fussy about such things!
(I’d flag this as an asnwer but SO wants me to wait a couple of days first)