How to reduce one day in existing date column in mysql database? - mysql

I have table tbl_dtcount. In that table there is one column for date.
Now I need to reduce one day for each and every rows in that date field. The date is beginning from 2012-05-19 to 2012-07-03. What is the MySQL update statement to perform this?

How about this.
Update tbl_dtcount
set mydate = DATE_SUB(mydate, INTERVAL 1 DAY)
where <conditions>;

UPDATE table_name
SET date_column = DATE_SUB('1998-01-02', INTERVAL 1 DAY)
....
see detail MySQL DATE_SUB

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

Truncate table with date time older than n days

I want to truncate some records from a table with date time clause.
This link have a solution, but it's not working for me.
TRUNCATE TABLE 'meter_to_sim_mapping' WHERE 'meter_to_sim_mapping'.'mapped_at' <=
In above where clause I want to add the date time value. The value is 2018-04-02 16:03:52. The query should delete all the records prior to this date.
How can I truncate the table with date-time?
Any help would be highly appreciated.
You need to use DELETE
DELETE FROM TABLE_NAME
WHERE DATE_COLUMN < NOW() - INTERVAL N DAY
or
DELETE FROM TABLE_NAME
WHERE CAST(DATE_COLUMN AS DATE) < STR_TO_DATE('1-01-2012', '%d-%m-%Y') - INTERVAL N DAY
In place of NOW() You can use your datetime value
STR_TO_DATE('12-01-2014 00:00:00','%m-%d-%Y %H:%i:%s')
Demo
http://sqlfiddle.com/#!9/4607a6/1
Below example may work for you:
DELETE FROM TABLE_NAME
WHERE DATE_COLUMN = DATE_SUB("2018-04-02 16:03:52", INTERVAL 10 DAY);
You can change it accordingly as per your requirement.
Reference: MySQL DATE_SUB() Function

SQL DATE extraction from a column

I have a table 'schedule' and column 'travel_date'.
travel_date is having 'a predefined date' in that.
I want to alter that column with '5days' more.
like
UPDATE Schedule SET travel_date=''+5days ;
I used
UPDATE schedule SET travel_date = (travel_date+5);
It worked how ?
In MySQL you can do that with
UPDATE customer
SET register_date = DATE_ADD(register_date, INTERVAL 5 DAY)
Why would you want to add 5 days to every customers register date???
Are you sure this is what you want to do?
UPDATE customer SET [register_date] = DATE_ADD([register_date], INTERVAL 5 DAY)
If it is a datetime column, use the DATE_ADD() function:
UPDATE customer SET register_date = DATE_ADD(register_date, INTERVAL 5 DAY)
Using DATE_ADD()
You can use the DATE_ADD() function to handle adding a given interval (e.g. days, minutes, hours, etc.) to an existing date column:
UPDATE customer
SET register_date = DATE_ADD(register_date, INTERVAL 5 DAY)
Using Date Arithmetic
Alternatively, you can simply use date arithmetic as well, which is similar to your previous example:
UPDATE customer
SET register_date = register_date + INTERVAL 5 DAY

How to delete old data?

I have a lot of old data. And I want to delete it.
Data example:
id ,title, date
What is the best way to delete old data using SQL? I mean delete row if too old, for example 1 month. How can I compare row's date with current time?
PS. I have date column. Look at the example.
Try sommething like this:-
DELETE FROM myTable WHERE [columndate]< DATE_SUB(NOW(), INTERVAL 1 MONTH);
If there isn't any row which stores when was the data inserted then it cannot be specified.
If there is a row named createddate,
you can try
delete from mytable where DATEDIFF(createddate, now())>30
OR you can try with
delete from mytable where createddate< DATE_SUB(NOW(), INTERVAL 1 MONTH);
DELETE * FROM TableName WHERE date < DATE();

How to Update all Dates in a Table

I have a table with 5 million DATETIME records. I want to add a year to all the various datetimes in the table. Can I do it all with a single query? Something like:
SELECT DATE_ADD(*, INTERVAL 1 YEAR);
Or any other way you would recommend. Thanks!
This should do what you want:
UPDATE table SET datefield = DATE_ADD(datefield, INTERVAL 1 YEAR);
If you need to update every table in the database check the answers to this question