SQL DATE extraction from a column - mysql

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

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

How to update timestamp

I'm now having trouble with using dateadd() function. Basically I want to add 1 day to cexpireday(timestamp) if it is at least 10 days after current day.
I have tried two ways but neither of them work
update card
set cexpireday = dateadd(day,1,cexpireday)
where cexpireday - current_timestamp() >= '0000-00-10 00:00:00'
and
update card
set cexpireday = date_add(cexpireday,INTERVAL 10 day )
where datediff(day,cexpireday,current_timestamp) >= 10
I got "dateadd does not exist" for the first and "incorrect parameter to navigate function datediff()" for the second.
Can anyone help me with this?
Maybe try this:
UPDATE card
SET cexpireday = DATE_ADD(cexpireday, INTERVAL 1 DAY)
WHERE cexpireday > DATE_ADD(NOW(), INTERVAL 10 DAY)
You need to use DATE_ADD instead of dateadd.
Also you need to have a look at documentation.
This is example how to use it DATE_ADD(date, INTERVAL 10 DAY).
Also DATEDIFF expects 2 params start_date and end_date and returns number of days between 2 dates.

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

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

How do I minus 2 hours from a date/time field in EVERY record in a table?

I'm trying to offset a timezone error from PHP. All times recorded in table 'test' was ahead by two hours. What I want is to update each record by minusing two hours from the time that is already there.
I tried:
UPDATE test
SET LastModifiedDate = SUBTIME( LastModifiedDate, '02:00:00' )
But this just updates all fields with the same value.
Please assist
tthanks
update test set LastModifiedDate = LastModifiedDate - interval 2 hour;
Use the DATE_SUB() function:
UPDATE test SET LastModifiedDate = DATE_SUB(LastModifiedDate, INTERVAL 2 HOUR)
Test it first to be certain it's doing what you want:
SELECT LastModifiedDate, DATE_SUB(LastModifiedDate, INTERVAL 2 HOUR) FROM test;
update test set LastModifiedDate = adddate(LastModifiedDate, interval -2 hour);
this will modify all your dates to -2 hour. you can narrow down the result in "where" section of the query by targeting specific rows.

Update date + one year in mysql

When I want setting numerical value +1 in mysql table, I use e.g.:
UPDATE table SET number=number+1 WHEN ...
How can I set date + one year?
Thanks
You could use DATE_ADD : (or ADDDATE with INTERVAL)
UPDATE table SET date = DATE_ADD(date, INTERVAL 1 YEAR)
This post helped me today, but I had to experiment to do what I needed. Here is what I found.
Should you want to add more complex time periods, for example 1 year and 15 days, you can use
UPDATE tablename SET datefieldname = curdate() + INTERVAL 15 DAY + INTERVAL 1 YEAR;
I found that using DATE_ADD doesn't allow for adding more than one interval. And there is no YEAR_DAYS interval keyword, though there are others that combine time periods. If you are adding times, use now() rather than curdate().
For multiple interval types use a nested construction as in:
UPDATE table SET date = DATE_ADD(DATE_ADD(date, INTERVAL 1 YEAR), INTERVAL 1 DAY)
For updating a given date in the column date to 1 year + 1 day