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
Related
current query
SELECT col1 FROM table1 where
id=1234 and (date(sys_time)
between "2015-07-01" AND "2015-07-10")
;
I want to get the data prior to one month from the dates mentioned
here. Is there any SQL query to do it?
Just use date_sub():
SELECT col1
FROM table1
WHERE id = 1234 AND
sys_time >= date_sub('2015-07-01', interval 1 month) and
sys_time < date_add(date_sub('2015-07-01', interval 1 month), interval 1 day)
Notice that I removed the date() function from sys_time. This helps MySQL use an index for the expression, if one is available:
Try this
SELECT GETDATE() AS CurrentDate, DATEADD(dd,-30,getdate())
You could set the "current date" and the previous date as parameters and include those in your query as the critera
Use DATE_ADD("2015-07-03", INTERVAL 1 MONTH) for the adding interval
Example :
SELECT a,b FROM table WHERE from_date between DATE_ADD("2015-07-01", INTERVAL 1 MONTH) AND DATE_ADD("2015-07-03", INTERVAL 1 MONTH)
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))
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Insert/ Update random date in MySQL
How to change a date change a date by +- 5 days randomly in mysql?
UPDATE student SET date = date*(RAND()*-5,+5)
SELECT 5 + ROUND(RAND()) * -10 will give either 5 or -5 which you can combine with the DATE_ADD() function.
Try this:
UPDATE `student` SET `date` = DATE_ADD(`date`, INTERVAL ((1 - ROUND((RAND()))*2)*5) DAY)
Following query outputs random date +-5 days from now (CURDATE())
select ADDDATE(CURDATE(), INTERVAL ROUND(RAND()*10)-5 DAY)
UPDATE student SET date = ADDDATE(date, INTERVAL ROUND((rand() * 9) - 4) DAY);
try something like this:
select case when (RAND()*10) >= 5 then date_add(<date_col>, INTERVAL 5 DAY)
else date_add(<date_col>, INTERVAL -5 DAY)
end
from <your_table>
SQL Fiddle demo
you Could Update your table like this:
UPDATE student SET `date`=
case when (RAND()*10) >= 5 then date_add(`date`, INTERVAL 5 DAY)
else date_add(`date`, INTERVAL -5 DAY)
end;
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
In MYSQL DB I need to check if a "datetime" field is more than 24hours (or whatever) ago in which case delete the row.
How to add hours to datetime in mysql?
thanks
Luca
What about something like this :
delete
from your_table
where your_field <= date_sub(now(), INTERVAL 1 day)
With :
now() : the current date time
date_sub() to substract 1 day to that date
Or, if you want o use 24 hours instead of 1 day :
delete
from your_table
where your_field <= date_sub(now(), INTERVAL 24 hour)
You have the Date and Time functions.
WHERE `yourDate` < DATE_SUB(NOW(),INTERVAL 1 DAY)
or shorter
WHERE `yourDate` < NOW() - INTERVAL 1 DAY
there is the addtime() method in mysql
DELETE
FROM table
WHERE datetime_field < DATE_SUB(NOW(), INTERVAL 1 DAY);