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
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
I have a query that selects records created from 1 hour in past from current time.
select ts from <table_name> where ts >= DATE_SUB(NOW(), interval 1 hour);
I can also select date before 7 days using
select count(*) from <table_name> where ts >= DATE_SUB(NOW(), interval 7 day);
How can I use these two date features to get records before 7 days from today and time 1 hour in past from current time.
For example, if the present time is 2015-11-06 10:03:00 then how can I get data for time between 2015-10-30 09:03:00 to 2015-10-30 10:03:00
I tried something like this, but it gives syntax error:
select ts from <table_name> where ts >= DATE_SUB(DATE(NOW()), INTERVAL 7 DAY), interval 1 hour)
select ts from <table_name> where ts >= DATE_SUB(NOW(), INTERVAL 7 DAY), interval 1 hour)
Your examples have syntax errors (too many closing parentheses )). If you want to use DATE_SUB(), you need to use it twice. To get entries between one time and another, use WHERE ... BETWEEN ... AND ...
You can use this:
SELECT ts
FROM iv_split_skill_metrics
WHERE ts BETWEEN
DATE_SUB(
DATE_SUB(DATE(NOW()), INTERVAL 7 DAY),
interval 1 hour)
AND
DATE_SUB(DATE(NOW()), INTERVAL 7 DAY)
Or, even better, skip DATE_SUB() entirely and just do subtraction, like this:
SELECT ts
FROM iv_split_skill_metrics
WHERE ts BETWEEN NOW() - INTERVAL 7 DAY - INTERVAL 1 HOUR
AND NOW() - INTERVAL 7 DAY
Edit: For some reason, you edited your question after I posted this and replaced iv_split_skill_metrics with <table_name> in your question, but the examples above will work regardless. Just use the correct table and column names, of course!
Edit 2: I see now that you want entries between 7 days plus 1 hour ago and 7 days ago. I have tweaked my answer to show you how to do that.
Your goal is not 100% clear but just my attempt:
SELECT ts
FROM table_name
WHERE ts >= DATE_ADD(DATE_ADD(NOW(), INTERVAL -7 DAY), INTERVAL -1 HOUR)
AND ts <= DATE_ADD(NOW(), INTERVAL -7 DAY);
but form performance perspective this query would be much faster:
http://sqlfiddle.com/#!9/9edd1/2
SET #end = DATE_ADD(NOW(), INTERVAL -7 DAY);
SET #start = DATE_ADD(#end, INTERVAL -1 HOUR);
SELECT ts
FROM table_name
WHERE ts BETWEEN #start AND #end;
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))
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);