Add date in timestamp MYSQL - mysql

Add date in timestamp
I want to run below query
UPDATE table
SET columnA = '2017-03-21 23:57:19'
WHERE .....
However, I want to construct the timestamp by taking whatever yesterday's date is, at 23:57:19 hours.
The datatype for columnA is timestamp. 2017-03-21 is yesterday's date. Am I supposed to use subdate(CURDATE(),1)?
UPDATE table
SET columnA = 'subdate(CURDATE(),1) 23:57:19' WHERE ..... ?
Or I have another way to do this ?

Use DATE_SUB()
UPDATE table
SET columnA = DATE_SUB('2017-03-22 23:57:19', INTERVAL 1 DAY)
or
UPDATE table
SET columnA = DATE_SUB(NOW(), INTERVAL 1 DAY)
If you wanted to get yesterday at 23:57:19 then try this:
DATE_ADD(TIMESTAMP(DATE_SUB(CURDATE(), INTERVAL 1 DAY)), INTERVAL '23:57:19' HOUR_SECOND)
DATE_SUB(CURDATE(), INTERVAL 1 DAY) - yesterday's date
TIMESTAMP(...) - resets yesterday to midnight
DATE_ADD(..., INTERVAL '23:57:19') - adds 23:57:19 to yesterday from midnight
Demo

Related

Set MySQL timestamp to random time within past 24 hours

I have a timestamp column in my MySQL table.
I'm wanting to set this timestamp to a random time within the past 24 hours for all rows in the table.
I know I can update all the rows doing this:
UPDATE table SET timestamp =
But I can't find if there's a way to set a random timestamp that's occurred within the past 24 hours so that each row has a different time.
You can use:
UPDATE table
SET timestamp = now() - interval floor((24*60*60)*rand()) second;
You can use Unixtimestqamps for that
UPDATE table1
SET timestamp = (SELECT TIMESTAMPADD(SECOND,
FLOOR(RAND() * TIMESTAMPDIFF(SECOND, NOW() - INTERVAL 1 DAY, NOW()))
, NOW() - INTERVAL 1 DAY));
You can try:
Update table set timestamp = select(cast((sysdate() - floor(rand()*24)) AS Datetime));
Check this might work for you.
update table name set timestamp = now() - interval floor(rand()*(60*60*24*2)) second;
Output:
you will get the current timestamp- between 0 seconds and two days.
If you want to change 2 days to 3 days or any days just need to change(60*60*24***Days enter here**))

Delete records older than 24 hours or 1 day based on timestamp

I'm trying to write mysql query to delete records older than 24 hours.
The SELECT sql statement which i used is below
SELECT * FROM Request WHERE
timeStamp <= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))
Table contains lot of records older than 1 day but the result of this sql query is empty. Also it doesn't show any error message.
Timestamp field structure is
Name: timeSatamp
Type: timestamp
Default: CURRENT_TIMESTAMP
Can somebody help me to find out the mistake in this statement?
Thanks in advance!
You dont need the FROM_UNIXTIME() so this will do what you want
SELECT * FROM `ts` WHERE timeStamp <= DATE_SUB(NOW(), INTERVAL 1 DAY)
Or
SELECT * FROM `ts` WHERE timeStamp <= NOW() - INTERVAL 1 DAY
you can use DATEDIFF instead of the comparing from two date.
SELECT * FROM Request WHERE DATEDIFF(timestamp, NOW())>=1;
DATEDIFF returns a number of days between two date/timestamp.
https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_datediff
Your Query should be like this:
SELECT * from Request where FROM_UNIXTIME(timeStamp) <= (NOW() - INTERVAL 1 DAY);
If the field timestamp contains 10 digits (like "1566836368") the right answer is
SELECT * from Request where FROM_UNIXTIME(timeStamp) <= (NOW() - INTERVAL 1 DAY);
like previously suggested by #akshaypjoshi
Delete 24 Hours older records using Mysql
DELETE FROM `table_name` WHERE `dateCreate` < (Now() - INTERVAL 24 HOUR);

In Mysql set date to one year later than current value, or one year from today

In mysql I had code to generate to 1 year + 1 day from today and then set expiryDate field (date datatype) to that for particular user
e.g
UPDATE fulllicense set expiryDate='2018-10-14' where userid=1;
but now I need to modify it so that if the current value of expiryDate is null or earlier than today to do as its currently doing but if later than today I want it to add a year and one day to the current value. So for example if the current value was 2017-12-17 I would want it set to 2018-12-18, I cannot work out how to do it in a single update statement
Use IF:
UPDATE fullllicense
SET expiryDate = DATE_ADD(DATE_ADD(IF(expirydate IS NULL OR expirydate < CURDATE(), CURDATE(), expirydate),
INTERVAL 1 YEAR),
INTERVAL 1 DAY)
UPDATE fulllicense
SET expiryDate =
CASE WHEN expiryDate IS NULL OR expiryDate < CURDATE()
THEN '2018-10-14'
ELSE
DATE_ADD(DATE_ADD(expiryDate, INTERVAL 1 YEAR), INTERVAL 1 DAY) END
I would use the DATE_ADD function. The WHERE clause it seems you already have handled, so I'll just use the one you included.
UPDATE fulllicense
SET expiryDate = DATE_ADD(DATE_ADD(expiryDate, INTERVAL 1 YEAR), INTERVAL 1 DAY)
WHERE userid = 1
AND expiryDate > NOW();
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

MYSQL - Trigger: IF Date is null, set date to CURDATE INTERVAL 1 DAY

Trying to build a simple trigger that modifies the MYSQL database in the following manner AFTER a new record is inserted in the TABLE:
Psudo code:
IF TABLE.AssignedDate IS NULL THEN
Set TABLE.DueDate = CURDATE() + INTERVAL 1
ELSE
SET TABLE.DueDate = AssignedDate + INTERVAL 1
I'm pretty sure I can't force the TIMESTAMP item from AssignedDate into the Interval 1 scenario. Any help would be appreciated
You could use date_add
Set TABLE.DueDate = date_add(CURDATE(), INTERVAL 1 DAY)

mysql yesterday date with specific timestamp

I am in a situation where i need to query data base to fetch records from yesterday with time stamp, for an example today is 25 so clause should be like:
STR_TO_DATE(created_date,'%Y-%m-%d') BETWEEN '2016-04-24 23:00:00' AND CURRENT_TIMESTAMP
I can get the previous date, but can't set it to 23:00:00 time, any idea's please?
I have tried:
SELECT
DATE_ADD(CURDATE(), INTERVAL - 1 DAY)
, CURRENT_TIMESTAMP
, HOUR(CURRENT_TIMESTAMP)
, DATE_ADD(
CURDATE() INTERVAL - ((HOUR(CURRENT_TIMESTAMP) + 1)) HOUR
)
But it gives an error, i tried to subtract the todays HOURS +1 from current timestamp.
Use
curdate() - interval 1 hour
Since curdate() gives you the current date WITHOUT time. Substract an hour from it and you get yesterday 23:00.