MySql query syntax error DATE_SUB - mysql

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.

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

Select date between current date and one week InnoDB

I have table with one column (date of expiration). I want to select all rows where expiration is between today and one next week. I am using MySQL and InnoDB type.
I try something like this:
SELECT name, expiration
FROM exp
WHERE (expiration BETWEEN(CURRENT_DATE, INTERVAL 1 WEEK));
But i have bad syntax. There is 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 ') LIMIT 0, 30' at line 3
Try this:
SELECT *
FROM exp
WHERE expiration BETWEEN
current_date
AND
current_date + interval 7 day
demo: --> http://www.sqlfiddle.com/#!2/8598a/2
This should be the syntax you want:
SELECT name, expiration FROM table WHERE expiration
BETWEEN
CURRENT_DATE
AND
ADDDATE(CURRENT_DATE, INTERVAL 1 WEEK)
(You need to add the one week to the current date, you cannot just specify the interval standing on its own/as a parameter to BETWEEN)

INSERT INTO SQL DATEADD Yesterday

I want to use PHP and MySQL to INSERT the the day of Yesterday. So my idea was:
INTO chartValues SET timestamp='1353369600', `datetime`=DATEADD(d,-1,GETDATE())
But its not working:
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 'INTO chartValues SET timestamp='1353369600',
datetime=DATEADD(d,-1,GETDATE())' at line 1
Thanks in advance
DATEADD and GETDATE() are T-SQL functions used in SQL Server.
You want to use DATE_ADD() or DATE_SUB() with NOW()
INSERT INTO chartValues SET timestamp='1353369600', `datetime`= DATE_SUB(NOW(), INTERVAL 1 DAY)
Reference
DATE_SUB(date, INTERVAL expr unit)
I believe you should use NOW() in MySql, instead of getdate(). See Mysql Date and Time Functions.
Date_add() not dateadd()
now() not getdate()
the link at the bottom is a good reference to mysql programming I recommend it.
so datetime = date_add(d,-1,now())
http://www.w3schools.com/sql/func_date_add.asp
You can use something like this in php:
date("F j, Y", time() - 60 * 60 * 24);
depending the datatype in your database you can change the "F j, Y" with the format that you need.
An in mysql something like this:
CAST(NOW() - INTERVAL 1 DAY AS DATE).

trying to get the date range members

I am trying to get the member details those who are notpaid by the given date range like this ((givendate+8 ,givendate+30) like this
I want the details like this the memebrs who are are not paid between these dates(((givendate+8 ,givendate+30))
like this if givendate is (1-09-2008) i want the date range like this (08-09-2008 ,30-09-2008)
i have done
WHERE DATE_ADD(memberpaysched.memberPaySched_dateDue,DATE_DIFF((ADDDATE(memberpaysched.memberPaySched_dateDue, 8)),(ADDDATE(memberpaysched.memberPaySched_dateDue,30))) )< NOW()
NOTE givendate = memberpaysched.memberPaySched_dateDue taking into account today date
but it was giving 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 'DATEDIFF((ADDDATE(memberpaysched.memberPaySched_dateDue, 8)),(ADDDATE(memberpays' at line 18
would any one pls give any idea about this
Try like this instead:
WHERE
memberpaysched.memberPaySched_dateDue BETWEEN
CURDATE() + INTERVAL 8 DAY
AND CURDATE() + INTERVAL 30 DAY
...
where
memberPaySched_dateDue
between '2008-09-01' + interval 7 day and '2008-09-01' + interval 29 day
Keep it simple and as close to how you would express it in English as possible:
WHERE memberPaySched_dateDue between ADDDATE(NOW(), 8) and ADDDATE(NOW(), 30)
or if you have a particular date instead of now():
WHERE memberPaySched_dateDue between ADDDATE('2011-09-01', 8) and ADDDATE('2011-09-01', 30)
Note that since the two between range values are constant during execution, this will be performant (unlike your code which forced a calculation every row)