Select date between current date and one week InnoDB - mysql

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)

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

Get data from some years back in mysql

I'm having a table in MySQL in which I keep the dates that the customer booked for the first time. I want to get all the customers that they booked 1 year back from that date.
The date is displayed like this:
How can I select the customers from 1 year back? I have seen multiple answers with DATEADD method like:
SELECT Kunde FROM `Buchungen` WHERE Buchungsdatum > date_add(yyyy, -1, GETDATE())
but it gives me this 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 '-1, GETDATE()) LIMIT 0, 30' at line 1
If I do:
SELECT Kunde FROM `Buchungen` WHERE Buchungsdatum > DATEADD(yyyy, -1, GETDATE())
then I get this:
1305 - FUNCTION db034.DATEADD does not exist
Any ideas? Thank you in advance!!
DATE_ADD(GETDATE(), INTERVAL -1 YEAR)
or
DATE_SUB(GETDATE(), INTERVAL 1 YEAR)
The link

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)

SQL and DateTime - How do I select all rows that have existed for a specific time period?

I have a table with a row named time of type DATETIME.
How can I select all rows which 'live' no more than 5 days?
Or in other words, how can I grab data which has timestamp not more then 5 days ago?
My request:
SELECT *
FROM `stats`
WHERE `domain` = 'test.com' AND DATEADD(NOW(), interval -5 day) > 'accept_date'
accept_date is the real name of a row. So , with that code I got:
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 ') > 'accept_date' LIMIT 0, 30' at line 1
It depends on the RDBMS but in MySQL it would be
where date_add(now(), interval -5 day) > time
or
where datediff(now(), time) < 5
The first means "120 hours have passed since", the second means "5 calendar days have passed since".
time is a terrible column name, by the way. Time of what?
'accept_date' is not the name of a column - it's a character literal.
You want
AND DATE_ADD(NOW(), interval -5 day) > accept_date
(note the missing single quotes ' around the column name)
GETDATE() is DB specific, yet:
select * from TABLE where (GETDATE() - time) <= 5

Compare dates in MySQL

I want to compare a date from a database that is between 2 given dates.
The column from the database is DATETIME, and I want to compare it only to the date format, not the datetime format.
SELECT * FROM `players` WHERE CONVERT(CHAR(10),us_reg_date,120) >= '2000-07-05' AND CONVERT(CHAR(10),us_reg_date,120) <= '2011-11-10'
I get this error when I execute the SQL above:
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
'us_reg_date,120) >= '2000-07-05' AND
CONVERT(CHAR(10),us_reg_date,120) <=
'2011-' at line 1
How can this problem be fixed?
You can try below query,
select * from players
where
us_reg_date between '2000-07-05'
and
DATE_ADD('2011-11-10',INTERVAL 1 DAY)
That is SQL Server syntax for converting a date to a string. In MySQL you can use the DATE function to extract the date from a datetime:
SELECT *
FROM players
WHERE DATE(us_reg_date) BETWEEN '2000-07-05' AND '2011-11-10'
But if you want to take advantage of an index on the column us_reg_date you might want to try this instead:
SELECT *
FROM players
WHERE us_reg_date >= '2000-07-05'
AND us_reg_date < '2011-11-10' + interval 1 day
This works:
select date_format(date(starttime),'%Y-%m-%d') from data
where date(starttime) >= date '2012-11-02';
Note the format string %Y-%m-%d and the format of the input date. For example 2012-11-02 instead of 12-11-2.
I got the answer.
Here is the code:
SELECT * FROM table
WHERE STR_TO_DATE(column, '%d/%m/%Y')
BETWEEN STR_TO_DATE('29/01/15', '%d/%m/%Y')
AND STR_TO_DATE('07/10/15', '%d/%m/%Y')
this is what it worked for me:
select * from table
where column
BETWEEN STR_TO_DATE('29/01/15', '%d/%m/%Y')
AND STR_TO_DATE('07/10/15', '%d/%m/%Y')
Please, note that I had to change STR_TO_DATE(column, '%d/%m/%Y') from previous solutions, as it was taking ages to load