Selecting data from database which is from this week except today - mysql

I need some help with some a simple query. I want to select data from a table where the date is in this week, but I don't want the records where the date is today. So if today is Monday, then I only want the data from Tuesday to Friday.
This was my best attempt.
SELECT *
FROM data
WHERE date > DATE_SUB(NOW(), INTERVAL 1 WEEK)
How can I improve this?

You can use between and subdate
SELECT * FROM `data` WHERE `date`
BETWEEN SUBDATE(CURDATE(), 7) AND SUBDATE(CURDATE(), 1);

You want to use CURDATE()
The CURDATE() function returns the current date.
Note: This function returns the current date as a "YYYY-MM-DD" format
if used in a string context, and as a YYYYMMDD format if used in a
numeric context.
And DATE()
The DATE() function extracts the date value from a date or datetime
expression.
SELECT * FROM data WHERE date > DATE_SUB(NOW(), INTERVAL 1 WEEK) AND DATE(date) < CURDATE();
This will effectively turn your data into YYYY-MM-DD and compare with today's YYYY-MM-DD

You can use between here and go one day back from today.
SELECT * FROM data WHERE date BETWEEN DATE_SUB(NOW(), INTERVAL 1 WEEK) AND DATE_SUB(CURDATE(), INTERVAL 1 DAY)
Be aware that I have used CURDATE() instead of NOW() as curdate does only contain the date without time.

Related

MySQL query to select records with date

I have a database where data_fine is defined as TEXT and contains values such as "25-05-2021". I need to find all the records between the current date up to 8 days.
I tried the following query, but nothing is displayed.
SELECT * from tabella_raw where data_fine > DATE(NOW) and data_fine < DATE(NOW() + INTERVAL 8 DAYS)
What is the best and safe way to compare the date stored as TEXT with the current date?
You have to convert dates which cost a lot of processor time, so you should avoid that and save all in MySQL date yyyy-MM-dd hh:mm:ss
Also you can use CURDATE() to get the the current date
Last the parameter fo INTERVAL IS DAYnot DAYS
SELECT STR_TO_DATE("25-05-2021",'%d-%m-%Y');
SELECT * from tabella_raw where STR_TO_DATE(data_fine,'%d-%m-%Y') > Curdate() and STR_TO_DATE(data_fine,'%d-%m-%Y') < CURDATE() + INTERVAL 8 DAY;
Try use STR_TO_DATE function
SELECT * from tabella_raw where STR_TO_DATE(data_fine, '%d-%m-%Y') > DATE(NOW) and STR_TO_DATE(data_fine, '%d-%m-%Y') < DATE(NOW() + INTERVAL 8 DAYS)
You are trying to use a text field as a date, so you need to convert the text to a date to use date functions.

select data on specific date

I want to retrieve records from db according to date format YYYY,MM,dd given by me but the column type is YYYY,MM,dd hh:mm:ss.
tried to use Date format function
SELECT *
FROM tabl.error_logs
where created_at = DATE_FORMAT(NOW(),'%Y-%m-%d'- INTERVAL 3 DAY);
I expect the created date will be 2019-06-08, but the result is empty
What is the actual datatype of created_at column?
This answer is going to ignore that funkiness with the format with commas, and assume that it's not character type data, and that it's DATETIME or TIMESTAMP.
Normative pattern for predicates on DATETIME and TIMESTAMP columns is a range comparison.
For example, to get all datetimecol values on June 10th, then something like this:
WHERE t.datetimecol >= '2019-06-10 00:00:00'
AND t.datetimecol < '2019-06-11 00:00:00'
Typically, I would just pass that one date value, and let MySQL figure out the next day. If we omit the time portion, MySQL will assume 00:00:00
WHERE t.datetimecol >= '2019-06-10' + INTERVAL 0 DAY
AND t.datetimecol < '2019-06-10' + INTERVAL 1 DAY
For performance, to allow MySQL to make effective use of a range scan operation on a suitable index, we want to avoid wrapping the column reference in a function. That is, specifying DATE(t.datetimecol) in a condition in the WHERE clause is going to force MySQL to evaluate the DATE() function on every row in the table.
With references to the bare column, that allows MySQL to make use of an index, if a suitable index is available.
e.g.
SELECT e.*
FROM tabl.error_logs e
WHERE e.created_at >= DATE(NOW()) + INTERVAL -3 DAY
AND e.created_at < DATE(NOW()) + INTERVAL -2 DAY
note that we can easily test those expressions in the WHERE clause, to verify they are returning what we want, and tweak as necessary:
SELECT DATE(NOW()) + INTERVAL -3 DAY
, DATE(NOW()) + INTERVAL -2 DAY
To make your query sargable, you need ...
SELECT *
FROM tabl.error_logs
WHERE created_at >= DATE_SUB(CURDATE(), INTERVAL 3 DAY)
AND created_at < DATE_SUB(CURDATE(), INTERVAL 2 DAY)
This selects all values of created_at on or after midnight three days ago, up to but not including < midnight two days ago. It uses a range scan on an index on created_at if one is available.
You coudl use date_sub()
SELECT *
FROM tabl.error_logs
where date(created_at) = DATE_SUB(date(now()), INTERVAL 3 DAY);
if the column created_at is a date then you could avoid the date() function and let the index (if present) work for this column
SELECT *
FROM tabl.error_logs
where created_at = DATE_SUB(date(now()), INTERVAL 3 DAY);

What is the right syntax for NOW() function

I'm trying to get all rows from DATE column
values from 10 days ago till today
i'm trying to undesrtand why this syntax isn't working:
select * from table WHERE date BETWEEN NOW() AND NOW() - INTERVAL 10 DAY ORDER BY date
You have to start with the lower value when using between
select *
from table
WHERE date BETWEEN NOW() - INTERVAL 10 DAY and NOW()
ORDER BY date
The problem is not related to the NOW() function but to the BETWEEN operator, the lower timestamp has to be specified first:
where date between now() - interval 10 day and now()
however, depending on your requirements, you might want to use this:
where date between current_date() - interval 10 day and current_date()
or just
where date>=current_date() - interval 10 day
now() returns a timestamp that contains date and time information, while current_date() returns just the current date without time information. If date is just a date column, without time information, using now() - interval 10 day you will get just the latest 9 days and not the latest 10 as you might expect.

mysql BETWEEN date range not working

I have a table called barcode_log, and these are all the datas from the table.
And now if I run this query
SELECT * FROM `barcode_log` WHERE barcode_log.assign_time BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) AND CURRENT_DATE;
I get this result
But it should return all the rows as all the data is within this month only. And assign_time field is stored as datetime. Any idea what i am doing wrong??
You are ignoring the time part (hh:mm:ss).
If the end day is set to the end timestamp of the current date then you can get the data of current day's too.
BETWEEN is inclusive
SELECT
*
FROM
`barcode_log`
WHERE
barcode_log.assign_time BETWEEN DATE_SUB(
CURRENT_DATE,
INTERVAL 30 DAY
)
AND TIMESTAMP(CONCAT(CURDATE(),' ','23:59:59'));
While the accepted answer works, there is a simpler solution. Just take the date part of the datetime column:
SELECT
*
FROM
`barcode_log`
WHERE
DATE(barcode_log.assign_time)
BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) AND CURRENT_DATE;
There is another way around: CAST() on barcode_log.assign_time field.
SELECT *
FROM `barcode_log`
WHERE CAST(barcode_log.assign_time AS DATE)
BETWEEN DATE_SUB(CURRENT_DATE, INTERVAL 30 DAY) AND CURRENT_DATE;
This excludes time from comparison and works fine for your purpose.

How to get dates which are between last monday till current day?

I am having dates in my database.
My database is in MySQL.
I want to fetch dates from my database which provides me dates from last monday till current day.
How can I do that?
You first have to work out how many days ago last monday was, using the DAYOFWEEK function, then subtract that from the current date -
SELECT * from table
WHERE date >= DATE_SUB(CURDATE(),INTERVAL MOD(DAYOFWEEK(CURDATE())-2,7) DAY)
AND date <= DATE_ADD(CURDATE(), INTERVAL MOD(7 - (DAYOFWEEK(CURDATE()) - 1), 7) DAY)
I'm not 100% sure about the +/- numbers here, you should be able to work it out from this though
EDIT: If this will only ever be run on the sunday at the end of the period, there is a much simpler version -
SELECT * from table
WHERE date >= DATE_SUB(CURDATE(), INTERVAL 6 DAY)
AND date <= CURDATE()
try this one
select * from table
WHERE date >date_sub(curdate(), interval WEEKDAY(curdate()) day) ;
You could always use the between function in your queries...
SELECT *
FROM orders
WHERE order_date between to_date ('2003/01/01', 'yyyy/mm/dd')
AND to_date ('2003/12/31', 'yyyy/mm/dd');
http://www.techonthenet.com/sql/between.php