Select exact expiry date - mysql

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))

Related

Mysql Add DATE_COLUMN 3 DAYS with query

I've been asking many question here, but it was dead end, I just want to make a simple select from date_column+3 day, I'm new to MySQL.
I've tried with this query
SELECT *
FROM pemesanan
WHERE pemesanan.`date` = DATE(DATE_ADD(date, INTERVAL 3 DAY))
but the result is empty, here's my column
date
2016-08-10
2016-08-04
2016-08-07
it must be show the 2016-08-10 data, but its not,
Can somebody enlighten me, I've been frustrated with this
If you use date right side you should use date left side
SELECT *
from pemesanan
where date(pemesanan.`date`) = DATE(DATE_ADD(NOW(), INTERVAL 3 DAY))
If you use the same date pemesanan.date you never get pemesanan.date = pemesanan.date +3
but if you want select the date older than 3 day you shuold use
SELECT *
from pemesanan
where date(pemesanan.`date`) <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))
and for delete
DELETE from pemesanan
where date(pemesanan.`date`) <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))
in your case
SELECT *
from pemesanan
where date(pemesanan.tanggal) <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))

Records between NOW() and 3am today or yesterday

How can I get records in a table between NOW() and the previous 3am?
This would be easy if it's 9am, but how do I write this if it's 2am? i.e I want the trades between DATE_SUB(CURDATE(), INTERVAL 21 HOURS) and NOW(). I'm looking for some code which can do both without needing to check the time in usercode and choose between two sql statements.
I'm sure there's a simple solution to this, but it's eluding me.
A simple idea is to subtract three hours and compare the date:
where date(date_sub(col, interval 3 hour)) = (case when hour(date) >= 3 then curdate() else date_sub(curdate(), interval 1 day)
Or, more explicitly, just do the comparison in SQL:
where (hour(date) >= 3 and date(col) = curdate()) or
(hour(date) < 3 and date(col) = date_sub(curdate(), interval 1 day)
I'll add my own answer (I finally gave up and explored using IF and found it could be used inside SELECT statements), but #Gordon Linoff might be better - I've no idea which of these is faster.
SELECT IF (NOW() > DATE_ADD(CURDATE(), INTERVAL 3 HOUR),
DATE_ADD(CURDATE(), INTERVAL 3 HOUR),
DATE_SUB(CURDATE(), INTERVAL 21 HOUR))
which can then be used as the conditional on an outer SELECT.

Select records where date == now + 21 days (NOT between)

I have a table session_dates with some fields and a timestamp field named timestart.
What I would like to do is select all the records from my table where the field timestart (TIMESTAMP) is equal to 21 days from now.
Like for example if today is 27 januari -> 17 februari.
I know how I can select all between two dates.
My SQL Query for between 2 dates:
SELECT timestart, timefinish, sessionid
FROM sessions_dates
WHERE timestart BETWEEN UNIX_TIMESTAMP(NOW()) AND UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 21 DAY))
But how to select equal to a date?
UPDATE:
I know now that I just have to use the = statement. But how can I test this? How do I know what the UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 21 DAY)) returns?
I think you want:
SELECT timestart, timefinish, sessionid
FROM sessions_dates
WHERE timestart >= UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 21 DAY)) AND
tmestamp < UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 22 DAY))
Presumably, timestart has a time component. This version takes that into account and still would allow the use of an index on timestart.

MySQL add days to a date

I have a table in MySQL. What would be the sql statement look like to add say 2 days to the current date value in the table?
UPDATE classes
SET
date = date + 1
where id = 161
this adds one second to the value, i don't want to update the time, i want to add two days?
Assuming your field is a date type (or similar):
SELECT DATE_ADD(`your_field_name`, INTERVAL 2 DAY)
FROM `table_name`;
With the example you've provided it could look like this:
UPDATE classes
SET `date` = DATE_ADD(`date` , INTERVAL 2 DAY)
WHERE `id` = 161;
This approach works with datetime , too.
UPDATE table SET nameofdatefield = ADDDATE(nameofdatefield, 2) WHERE ...
This query stands good for fetching the values between current date and its next 3 dates
SELECT * FROM tableName
WHERE columName BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 3 DAY)
This will eventually add extra 3 days of buffer to the current date.
You can leave date_add function.
UPDATE `table`
SET `yourdatefield` = `yourdatefield` + INTERVAL 2 DAY
WHERE ...
update tablename set coldate=DATE_ADD(coldate, INTERVAL 2 DAY)
For your need:
UPDATE classes
SET `date` = DATE_ADD(`date`, INTERVAL 2 DAY)
WHERE id = 161
DATE_ADD(FROM_DATE_HERE, INTERVAL INTERVAL_TIME_HERE DAY)
will give the Date after adjusting the INTERVAL
eg.
DATE_ADD(NOW(), INTERVAL -1 DAY) for deducting 1 DAY from current Day
DATE_ADD(NOW(), INTERVAL 2 DAY) for adding 2 Days
You can use like
UPDATE classes WHERE date=(DATE_ADD(date, INTERVAL 1 DAY)) WHERE id=161
SELECT DATE_ADD(CURDATE(), INTERVAL 2 DAY)
SET date = DATE_ADD( fieldname, INTERVAL 2 DAY )
SELECT ADDDATE(d,INTERVAL 1 DAY)
from table

How do I get Timestamp minus 6 weeks in MySQL?

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)