i tried to use these mysql function for persian date
i got this error but i couldnt solve it
SQL query:
CREATE FUNCTION `persian_day` (
indate date
) RETURNS int( 11 ) BEGIN declare j int;
MySQL said: Documentation
#1064 - 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 3
i use mysqlnd 5.0.8-dev with phpmyadmin (xampp package)
here is my function
CREATE FUNCTION `persian_mod`(a double,b double) RETURNS int(11)
return (a - b * floor(a / b))
CREATE FUNCTION `persian_div`(a double,b double) RETURNS int(11)
return (floor(a / b))
CREATE FUNCTION `persian_pj`(y int,m int,d int) RETURNS int(11)
return ( 227260 +
1029983 * persian_div((y - 474), 2820) +
365 * ( (persian_mod((y - 474), 2820) + 474) - 1) +
persian_div(682 * (persian_mod((y - 474), 2820) + 474) - 110, 2816) +
if(m > 6, 30 * m + 6, 31 * m)
+ d )
CREATE FUNCTION `persian_day`(indate date) RETURNS int(11)
begin
declare j int;
declare a int;
declare b int ;
declare c int;
declare d int;
declare yearr int;
declare f double;
declare monthh int;
set j=TO_DAYS(indate) ;
set a = j - persian_pj(475, 0, 1) ;
set b = persian_div(a, 1029983) ;
set c = persian_mod(a, 1029983) ;
set d = if(c <> 1029982, persian_div(2816 * c + 1031337, 1028522), 2820) ;
set yearr = 474 + 2820 * b + d;
set f = (1 + j) - persian_pj(yearr, 0, 1);
set monthh= if(f > 186, ceil((f - 6) / 30) - 1, ceil(f / 31) - 1);
return (j - (persian_pj(yearr, monthh, 1) - 1));
end
why this error happen?and how to solve it?
The basic syntax when creating a function is that you should have a return statement.
but if you have special calculation (of course it’s multiline) inside your function, you need to change the delimiter first:
back in your case,
you have not
RETURNvalue and you haven’t close the function usingENDkeyword. Just like what you did on the other functions you have created.CREATE FUNCTION Example