Change a date randomly by +- 5 days in mysql? [duplicate] - mysql

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;

Related

Declaring and populating Attributes on a table MySQL [duplicate]

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

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

return records between yesterday and last 7 days + mysql

here is the code I am using to return past 24 hours records
SELECT *
FROM mytable
WHERE CASE WHEN `created` > DATE_SUB(NOW(), INTERVAL 1 DAY) THEN 1 ELSE 0 END
how to return records between yesterday and last 7 days
Use the BETWEEN operator.
CASE WHEN created BETWEEN DATE_SUB(NOW(), INTERVAL 7 DAY) AND DATE_SUB(NOW(), INTERVAL 1 DAY)
THEN 1
ELSE 0
END
Try this, it works without case statement, so should be faster:
SELECT *
FROM mytable
WHERE created BETWEEN date(CURRENT_TIMESTAMP-7) AND date(CURRENT_TIMESTAMP-1);

How to return the items in MYSQL greater than 6 months?

How do I return items in MYSQL based on the Timestamp column great than six months and delete them? I'd like to be able to delete items in the table that are greater than six months so that the table doesn't keep growing .. what's the exact query for that?
Here's a query to find all rows older than 6 months based on the value of a timestamp column:
select id
from your_table
where your_timestamp_column <= (now() - interval 6 month);
Try this:
Using DATEDIFF function:
SELECT * FROM tableName
WHERE DATEDIFF(CURDATE(), colName) > 180;
DELETE FROM tableName
WHERE DATEDIFF(CURDATE(), colName) > 180;
Using DATE_SUB function:
SELECT * FROM tableName
WHERE colName < DATE_SUB(CURDATE(), INTERVAL 6 MONTH);
DELETE FROM tableName
WHERE colName < DATE_SUB(CURDATE(), INTERVAL 6 MONTH);

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