How do i extract all rows greater then 7 days of a start date?, I'm trying to use this query in MySQL. Below is my statement.
SELECT * from v_polygons a
INNER JOIN tblProjectData z
on z.Project_ID = a.Project_ID
WHERE DATE_ADD(z.FlyDate, INTERVAL 7 DAY) > NOW() + INTERVAL rge DAY
I have a start date z.FlyDate, So i give it +7 days, then i check to see if that date is greater then NOW()
is this correct or have i messed it up?
You can just do:
WHERE DATE_ADD(DATE(z.FlyDate), INTERVAL 7 DAY) < DATE(NOW());
This will ignore the time part. You can remove DATE function call if you want to consider the time as well.
Related
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.
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)
I have this query
SELECT * FROM `timeclock_timecard`
WHERE `clock_in_datetime` > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
which can get record in the last day but I need to limit to records created after 7AM
Any help please?
SELECT * FROM `timeclock_timecard`
WHERE `clock_in_datetime` > DATE_SUB(CURDATE(), INTERVAL 1 DAY)
and hour(`clock_in_datetime`) > 7;
Added one more filter condition to check for the hour.
Your query was almost correct, because CURDATE() only gives the date you can just subtract 17 hours to get the correct result. fiddle.
SELECT * FROM `timeclock_timecard`
WHERE `clock_in_datetime` >= DATE_SUB(CURDATE(), INTERVAL 17 HOUR)
To get the entries of the current day, we can add 7 hours (CURDATE() has time 0:00).
SELECT * FROM `timeclock_timecard`
WHERE `clock_in_datetime` >= DATE_ADD(CURDATE(), INTERVAL 7 HOUR)
To get only rows from yesterday, with a time value of 7AM or later, we can add 7 hours to the expression.
If we only up until midnight of today (just rows from yesterday), we can add another condition, the datetime is less than midnight today.
For example:
SELECT t.*
FROM `timeclock_timecard` t
WHERE t.`clock_in_datetime` >= DATE(NOW()) + INTERVAL -1 DAY + INTERVAL 7 HOUR
AND t.`clock_in_datetime` < DATE(NOW())
If you want to exclude the exact 7:00:00 AM value, change the >= to just >.
FOLLOWUP
Q: What I actually want is between about 5-6am TODAY and mindnight TODAY so anytime during today that I run the report for today I will get only timeclock data from users who clocked in/out today only and not include yesterdays data.
A: The predicates are going to be of the form
WHERE t.`clock_in_datetime` >= expr1
AND t.`clock_in_datetime` < expr2
You just need to find the expressions expr1 and expr2 that return the appropriate datetime values.
Just use a simple SELECT statement to test:
SELECT DATE(NOW()) + INTERVAL 5 HOUR AS `start`
, DATE(NOW()) + INTERVAL 1 DAY AS `end`
Q: I also modified my select to take in account my datetime is in UTC and my result needs to get todays records using local timezone.
SELECT * , CONVERT_TZ( clock_in_datetime , '+00:00', '-4:00' ) FROM `timeclock_timecard`
A: Personally, I would do the timezone conversion on the exprN values, not the column values. Having predicates on bare columns allows MySQL to make effective use of an index; wrapping the columns in expressions prevents MySQL from using an index.
If the MySQL system clock is UTC, and your datetime values stored in the table are in a different timezone, yes, use the MySQL CONVERT_TZ function.
Again, using a simple SELECT statement to develop and test the expressions:
SELECT CONVERT_TZ( DATE(NOW()) + INTERVAL 5 HOUR, '+0:00', to_tz) AS `start`
, CONVERT_TZ( DATE(NOW()) + INTERVAL 1 DAY , '+0:00', to_tz) AS `end`
Where to_tz is the timezone of the values in the table.
Once you get expressions start and end returning the values you need, then use those expressions in the predicates of the query of the timecard table.
Here is my problem, I want to fetch next 30 days records from the table. I have a field in my table. For ex: In my table I have resource_date, In this column I have many records from 2013-02-05 to 2015-10-10. Say, If I logged into the website today(Today's Date is- 16/01/2015, It should fetch record for next 15 days and so on). How to do this? Thanks in advance
One way to do it
SELECT *
FROM table1
WHERE resource_date >= CURDATE() + INTERVAL 1 DAY -- skip today
AND resource_date < CURDATE() + INTERVAL 17 DAY -- 15 days starting tomorrow
Here is a SQLFiddle demo
In MySQL, you can use the NOW() function to get the current DATETIME, and the INTERVAL keyword to get intervals of time.
So, to get the records where resource_date is within the next 30 days, you would use:
SELECT *
FROM `my_table_name`
WHERE `resource_date` >= NOW()
AND `resource_date` < NOW() + INTERVAL 1 MONTH
;
In practice, you should rarely use SELECT *, and you should consider adding a LIMIT to this query to prevent your application from returning a result set that is "too large".
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
...
WHERE
'resource_date'> NOW() AND
'resource_date'< DATE_ADD(NOW(), INTERVAL 31 DAY);
Careful I think now() does minutes and hours so you miss a portion of a day.
WHERE resource_date >= CURDATE() AND resource_date <= DATE_ADD(CURDATE(), interval 15 DAY)
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);