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

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

Related

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.

Data from current date to 30 days after current days

Why this query is not working
SELECT * FROM history WHERE DATE(date) < CURDATE() + 30
I am trying to get the data from 30 days but my query is not working.Why
What does +30 mean? Days? Years? Months? Hours? You need to use (the proper syntax) a format MySQL understands:
SELECT * FROM history WHERE DATE(date) < CURDATE() + INTERVAL 30 DAY
To get the data from today on to 30 days after current day, you've got to set an upper and an lower limit, so use:
SELECT * FROM history WHERE
date >= CURDATE()
AND
date < CURDATE() + INTERVAL 31 DAY
Please note that by not using a function on your date column you won't prohibit MySQL to use an index on this column.
The lower limit should be obvious, the upper limit means that you've got the complete day that's 30 days later than today. If you use + INTERVAL 30 DAY instead this last day is excluded from the result.
Because you're not using the right construct, try:
SELECT * FROM history WHERE DATE_ADD(date, INTERVAL 30 DAY);

Select records from NOW() -1 Day

Is there a way in a MySQL statement to order records (through a date stamp) by >= NOW() -1 so all records from the day before today to the future are selected?
Judging by the documentation for date/time functions, you should be able to do something like:
SELECT * FROM FOO
WHERE MY_DATE_FIELD >= NOW() - INTERVAL 1 DAY
Be aware that the result may be slightly different than you expect.
NOW() returns a DATETIME.
And INTERVAL works as named, e.g. INTERVAL 1 DAY = 24 hours.
So if your script is cron'd to run at 03:00, it will miss the first three hours of records from the 'oldest' day.
To get the whole day use CURDATE() - INTERVAL 1 DAY. This will get back to the beginning of the previous day regardless of when the script is run.
Didn't see any answers correctly using DATE_ADD or DATE_SUB:
Subtract 1 day from NOW()
...WHERE DATE_FIELD >= DATE_SUB(NOW(), INTERVAL 1 DAY)
Add 1 day from NOW()
...WHERE DATE_FIELD >= DATE_ADD(NOW(), INTERVAL 1 DAY)
You're almost there: it's NOW() - INTERVAL 1 DAY
Sure you can:
SELECT * FROM table
WHERE DateStamp > DATE_ADD(NOW(), INTERVAL -1 DAY)
when search field is timestamp and you want find records from 0 hours yesterday and 0 hour today use construction
MY_DATE_TIME_FIELD between makedate(year(now()), date_format(now(),'%j')-1) and makedate(year(now()), date_format(now(),'%j'))
instead
now() - interval 1 day

Need mySQL TIMESTAMP for begining of the current week or any given date?

I need mySQL timestamp for start of current week or any given date, if week starts with monday?
I'm trying something like:
SELECT UNIX_TIMESTAMP(CONCAT( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(DATE_SUB(CURDATE(), INTERVAL 7 DAY)) DAY), ' 00:00:00')) as start
Monday has a dayofweek index=2. DAYOFWEEK($date) gives the index of the day (1-7, Sun-Sat). So, you need to add or subtract days from your $date's index to change it to 2.
e.g.
SELECT UNIX_TIMESTAMP(
CASE WHEN DAYOFWEEK($date)>=2
THEN DATE_SUB($date, INTERVAL (DAYOFWEEK($date)-2) DAYS)
ELSE DATE_ADD($date INTERVAL 1 DAY)
END
);
I think I've got the syntax right on this, but check CASE and date and time functions.