MYSQL - Cannot create event scheduler - mysql

iam using MYSQL maria DB and run script
CREATE EVENT 'delete_expire_sku_permonth'
ON SCHEDULE EVERY 1 MONTH STARTS '2017-11-23 00:00:00'
DO BEGIN
DELETE FROM tablestok WHERE system_update < DATE_SUB(NOW(), INTERVAL 30 DAY
END
and have error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''delete_expire_sku_permonth'
ON SCHEDULE EVERY 1 MONTH STARTS '2017-11-23 00:00' at line 1
What the correct answer ?

I think it's because of the single quotes you put in this line CREATE EVENT 'delete_expire_sku_permonth'. Please try running the same without the single quotes.

Related

MySQL equivalent of SQL Server syntax?

I'm new on MySQL. I have below SQL Query and trying convert into mysql to give a default value for a date column in the table create script.
(date_add(second,(-1),sysutcdatetime()))
But it doesn't support in mysql. Getting below error.
Error Code: 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 '(-1),sysutcdatetime()))' at line 1
Also I need to get the date in this format '9999-12-31 23:59:59.999999'
This probably does what you want:
now() - interval 1 second
However, if you do specifically want UTC, then:
utc_timestamp() - interval 1 second
SELECT DATE_ADD(sysutcdatetime, INTERVAL -1 second);
Example:
SELECT now(),DATE_ADD(now(), INTERVAL -1 second);
now()
DATE_ADD(now(), INTERVAL -1 second)
2021-03-17 11:43:32
2021-03-17 11:43:31
db<>fiddle here

MySQL (Aurora) update timestamp using adddate or date-add or date-sub says syntax error

Can any of you figure out what is the syntax error on mySQL update statement here ?
update table_name set start_time = DATE_ADD (start_time , INTERVAL 2 DAY) where start_time = '2020-12-08 10:47:00';
The above is simple mySQL query to update start_time (time stamp) by 2 days.
AFAIK, the above should work and its nothing complicated. But i am getting syntax error and i am not able to comprehend why there is a syntax error. Here is the error i got...
Database error code: 1064. Message: 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 ') where start_time = '2020-12-08 10:47:00'' at line 10
I have also tried other variants such as...
update table_name set start_time = adddate (start_time , INTERVAL 2 DAY) where start_time = '2020-12-08 10:47:00';
update table_name set start_time = date_sub (start_time , INTERVAL -2 DAY) where start_time = '2020-12-08 10:47:00';
Database error code: 1064. Message: 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 ') where start_time = '2020-12-08 10:47:00'' at line 3
If i use the date_sub in select statement,
select * from table_name where start_time > DATE_SUB(now() , INTERVAL 2 DAY);
it does work like a charm. But i am not able to use it in update statement for set =....
I am just not able to comprehend what is going wrong and curious to know what is wrong with the syntax. Can you suggest ?
It seems like the problem is the space between the function name and the opening parenthese.
This:
DATE_ADD (start_time , INTERVAL 2 DAY)
Should be written:
DATE_ADD(start_time , INTERVAL 2 DAY)
This is not happening in the example using DATE_SUB(), because the leading space is not there.
Not all MySQL functions have such restriction. This is related to the way the MySQL parser handles function names, which is described in the documentation:
The requirement that function calls be written with no whitespace between the name and the parenthesis applies only to the built-in functions that have special considerations.
DATE_ADD() and DATE_SUB() are listed as affected functions. You can play around with SQL mode IGNORE_SPACE to change the default behavior.
Overall, I would recommend using date arithmetic like so, so you don't need to worry about such caveat (and it makes for more readable code as well):
start_time + INTERVAL 2 day

Error making an MySQL event

I've got a MySQL table that needs to be emptied every midnight due to information in there being used for sessions.
I've tried to create a MySQL event after turning on the global parameter but it gives me a Syntax error as soon as I want to create one.
This is the error I'm getting.
CREATE EVENT delete_messages_at_midnight
ON SCHEDULE EVERY 1 DAY
STARTS CURDATE() + INTERVAL 1 DAY
DO DELETE FROM t_choices;
delimeter;
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'delimeter' at line 4
Due to me being new here and having lower than 10 rep I can't post images unfortunantly.
Try to remove the semicolon after:
t_choices;
and simply try
delimiter
CREATE EVENT delete_messages_at_midnight
ON SCHEDULE EVERY 1 DAY STARTS CURDATE() + INTERVAL 1 DAY
DO DELETE FROM t_choices
delimeter ;
or like this:
delimiter
|
CREATE EVENT delete_messages_at_midnight
ON SCHEDULE EVERY 1 DAY STARTS CURDATE() + INTERVAL 1 DAY
DO DELETE FROM t_choices;
|
delimeter ; //Put a space between delimiter and semicolon

MySQL Event For Deleting Old Records throws error #1064

I am trying to create an event in MySQL (my first time) that will delete all the records in the "reports" table that are older than 3 months.
The event should run every day at 3:00AM. The reports table has the field "created" of type DATETIME.
This is the code I came up with for creating the event:
CREATE
EVENT `delete_reports`
ON SCHEDULE
EVERY 3 DAY_HOUR
DO BEGIN
DELETE FROM `reports` WHERE `created` < DATE_SUB(NOW(), INTERVAL 3 MONTH)
But this throws the following error:
#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 6
I understand the 1064 error refers to a syntax problem, but I after looking up several different examples I still haven't managed to get this to work.
Any suggestions would be really appreciated!
You're missing an END and with that you should change the delimiter. Or you omit the BEGIN.
So, either like this:
DELIMITER $$
CREATE EVENT `delete_reports`
ON SCHEDULE
EVERY 3 DAY_HOUR
DO BEGIN
DELETE FROM `reports` WHERE `created` < DATE_SUB(NOW(), INTERVAL 3 MONTH);
END $$
DELIMITER ;
or like this:
CREATE EVENT `delete_reports`
ON SCHEDULE
EVERY 3 DAY_HOUR
DO
DELETE FROM `reports` WHERE `created` < DATE_SUB(NOW(), INTERVAL 3 MONTH);

MySql query syntax error DATE_SUB

what's wront with this query?
SELECT *
FROM containmentTracker
WHERE reviewDate < NOW()
AND reviewDate > DATE_SUB(NOW(), INTERVAL 10 YEARS)
I've tried in several way but every time I use DATE_SUB I get
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 'YEARS)' at line 1
or similar errors.
What I'm doing wrong?
Thank you
The unit to INTERVAL is always singular which makes it sound kind of odd when reading the query aloud:
DATE_SUB(NOW(), INTERVAL 10 YEAR)
See DATE_ADD() in the reference manual.