I am trying to compare timestamps by finding out if particular timestamp field is greater than 15 days ago.
So if I try this on my database, I get:
SELECT DATE_SUB(NOW(), INTERVAL 15 DAY) // 2012-04-08 11:00:54
The field concerned has a value: 2012-04-20 21:18:14.
So when I try:
SELECT * FROM (`payments`) WHERE `LastUpdated` >= 'DATE_SUB(NOW(), INTERVAL 15 DAY)'
I get no rows returned? Am I comparing dates incorrectly?
You are treating the DATE_SUB function as a string.
Try this instead:
SELECT * FROM (`payments`) WHERE `LastUpdated` >= DATE_SUB(NOW(), INTERVAL 15 DAY)
Related
Given a set of data with date_created stored like 2017-04-13 23:29:52, how would I construct an SQL query to select all items that were created within the last 3 hours?
I originally thought to do something like this:
SELECT
*,
MAX(date_created)
FROM items
GROUP BY date_created
but that would not be exactly what I want. I'm not sure how to go about this.
Use NOW() and INTERVAL in your WHERE clause
SELECT * FROM items WHERE date_created <= NOW() - INTERVAL 180 minute AND date_created >= NOW() - INTERVAL 210 minute
This one uses CURDATE, CURDATE and DATE_ADD:
SELECT *
FROM items
WHERE DATE(date_created) = CURDATE()
AND TIME(date_created) BETWEEN CURTIME() AND DATE_ADD(CURTIME(), INTERVAL -3 HOUR)
select * from items where
extract(hours from age(current_timestamp, date_created))>=3;
Extract keyword would extract hours difference from current timestamp and return only that is greater than or equal to 3.
I have this record in expiry_date column:
2015-04-30 04:15:29
2015-04-22 06:02:07
I need to select where the record is 26 days from expiring. Right now I'm using this which is not working. No records were selected.
SELECT * FROM `client` WHERE `expiry_date` = DATE_ADD(NOW(), INTERVAL 26 DAY)
I've searched this website and many of the answers are using <= operator. This solution partially work. It selects both of my record when I only need 2015-04-30 04:15:29 in expiry_date column.
How do I exactly select date that is going to expired and not all date?
The easy solution to this is to use the date function:
WHERE DATE(expiry_date) = DATE_ADD(CURRENT_DATE, INTERVAL 26 DAY)
However, this prevents the use of an index on expiry_date. An alternative that does work with indexes is:
WHERE expiry_date >= DATE_ADD(CURRENT_DATE, INTERVAL 26 DAY) AND
expiry_date < DATE_ADD(CURRENT_DATE, INTERVAL 26 + 1 DAY)
The reason you're having this issue is that expiry_date is a type of datetime so the time makes it not equal. Just change your code to be:
SELECT * FROM client WHERE DATE(expiry_date) = DATE(DATE_ADD(NOW(), INTERVAL 26 DAY))
Say i have an expiration date field, called expires_at, and lets say i have 1 row with 1 value whitch is 10 days from today.
How do i query all rows where expires_at is no more then 14 days from now or before that?
Something like this i asume
SELECT * FROM column WHERE CURDATE() +14? >= expires_at
Can i add 14 days to the current date and just get all the days before CURDATE() + 14?
I am not awesome at SQL
SELECT * FROM your_gtable
WHERE expires_at <= CURDATE() + interval 14 day
... WHERE expires_at < DATE_ADD( NOW(), INTERVAL 14 DAY)
I have a field named timestamp. This is the last time a member was logged in.
I am looking to include a where clause in a query for something like
WHERE timestamp > todays date - 6 weeks
How would I do this?
I am trying to only include users that have logged in in the last 6 weeks.
Thanks
I find this syntax more readable than date_sub, but either way works.
WHERE timestamp >= NOW() - INTERVAL 6 WEEK
If you want to go by "Today" (midnight) instead "now" (current time), you would use this
WHERE timestamp >= DATE(NOW()) - INTERVAL 6 WEEK
where column>=date_sub(now(), interval 6 week)
This link demonstrates how you might acquire a timestamp of yesterday using the format DATE_ADD(CURDATE(), INTERVAL -1 DAY), therefore your query would probably be:
WHERE timestamp > DATE_ADD(CURDATE(), INTERVAL -42 DAY)
You can use between and now():
select somevalue
from yourtable
where yourtimestamp between now() - interval 1 day and now()
for TIMESTAMP there is a TIMESTAMPADD() function
SELECT TIMESTAMPADD(WEEK, -6, CURRENT_TIMESTAMP)
this will return the timestemp of 6 weeks ago
or in the case like the question
SELECT * FROM users
WHERE lastlogin > TIMESTAMPADD(WEEK, -6, CURRENT_TIMESTAMP)
Any luck yet. Have you tried:
>= DATE_SUB(NOW(), INTERVAL 6 WEEK)
I am storing rows with MySQL using a date col. Everytime I insert a new row I use date=CURDATE() .
Is there anyway I can run a SELECT statement that selects all rows that were created within the last 24 hours?
Joel
Try this:
SELECT * FROM table WHERE date >= DATE_SUB(NOW(), INTERVAL 24 HOUR))
You can use the sub_date() function with interval to make a range of dates between now and 24 hours ago:
SELECT * FROM
table WHERE
date_field BETWEEN curdate() AND DATE_SUB(curdate(), INTERVAL 1 DAY);