Get specific date range - mysql

I'm using
SELECT * from tbl_name WHERE DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)
to select data for specific days. The problem is that line gets data right before 3 days.
What to do so selected data to be period three days before till now ?

First your field should be of type datetime or date and then you can use a between clause
your_date_field BETWEEN now() - INTERVAL 72 HOURS AND now()

Related

Get Remaining Days in MYSQL SELECT (Subtracting 3 datetimes)

I'm trying to subtract now() - the created datetime field from 30 days to get the days remaining as a datetime field, mysql gives me an error for this sort of thing.
SELECT id, created, INTERVAL 30 DAY - CURRENT_DATE - created as timeleft FROM tablename
Use the Date add function to subtract 30 days like this.
SELECT DATE_ADD(current_date, interval -30 day);
Return the difference in days between two date values:
SELECT DATEDIFF(current_date, "2017-06-15");
You can use the combination of these functions to achieve the desired result.
In mysql you should translate the datetime to unix_timestamp to calculate the difference between the two days
SELECT id, created, 30 - (unix_timestamp(CURRENT_DATE) - unix_timestamp(created )) / 3600/24 as timeleft FROM tablename

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.

Extract only month and day from date field mysql

I've a table in a db with some date field with format yyyy-mm-dd
I'm trying to perform a query that take just records with a interval of 3 month from today.
I've done like this
WHERE DATE_SUB(myTable.myField, INTERVAL 3 MONTH) = CURDATE()
and it works, but my second step is ignore years of my date field and from curdate().
I've tried EXTRACT or DATEFORMAT, but query doesn't work with those function.
How can I modify my query?
Thanks
The condition is wrong.
Try this instead:
...WHERE myTable.myField >= CURDATE() - INTERVAL 3 MONTH...
EDIT:
Based on your comment:
with my query i've got all record that have in dateField this date
'2016-12-07' (curdate() is today '2016-09-07') and it's fine. but i
want that query gives me also date that have 12 on month and 07 on
day, ignoring year. Eg. if i have '2016-12-07' and '2014-12-07', my
query must give me both records. it's a query that will run every day
...WHERE DATE_FORMAT(myTable.myField,'%m-%d') =
DATE_FORMAT((CURDATE() + INTERVAL 3 MONTH),'%m-%d')...
Use below condition which will find records with a interval of 3 month from today.
WHERE myTable.myField = DATE_SUB(CURDATE(), INTERVAL 3 MONTH)

mysql date functions

I have a date field in the database table of this format 2012-02-1.i need to write 3 different queries:
a.) I need to retrieve all fields where date is between today and previous 5 days.
b.) I need to retrieve all fields where date is older than 5 days from today's date.
c.) I need to retrieve all fields where date between '5 days ago' to '30 days ago'
Can I use some inbuilt mysql function.
Manipulating the query below:
SELECT fields
FROM table
WHERE date >= CURDATE() - 5
or something like this
Or using a between clause. I am not getting the syntax correct.
SELECT p.status,p.downpayment_date,p.policy_id,i.id,i.policy_type,i.carrier,i.policy_nu‌​mber,i.client_id,c.id,c.client_name FROM pdp_payment AS p,pdp_policy_info AS i,pdp_client_info AS c WHERE p.policy_id=i.id AND i.client_id=c.id AND (((p.status='close pending') OR (p.status='Cancel')) AND (p.downpayment_date BETWEEN ((INTERVAL 5 DAY AND CURDATE()) - (INTERVAL 30 DAY AND CURDATE()))) )
Date between today and previous 5 days.
SELECT fields FROM table
WHERE date_field BETWEEN CURRENT_DATE - INTERVAL 5 DAY AND CURRENT_DATE
Date smaller than previous 5 days.
SELECT fields FROM table
WHERE date_field < CURRENT_DATE - INTERVAL 5 DAY
For all fields where date is between today and previous 5 days.
SELECT fields
FROM table
WHERE your_date_field_name BETWEEN CURDATE() - INTERVAL 5 DAY AND CURDATE()
You can work out other problems in a similar way
date is a keyword, so when you use it as a field name it MUST be enclosed in backticks ` otherwise you will get a parse error.
To get the range you want:
WHERE `date` BETWEEN DATE_ADD(NOW(),INTERVAL -30 DAY) AND DATE_ADD(NOW(),INTERVAL -5 DAY)