MySQL add 12 hours to a time field - mysql

I need to add 12 hours to a MySQL TIME field (not DATETIME) and I'm having trouble.
UPDATE `events`
SET start_time = DATE_ADD(start_time, INTERVAL 12 HOUR)
WHERE `start_time` < '11:00:00'
returns with no errors but doesn't change anything, I think because start_time is a TIME field.
UPDATE `events`
SET start_time = start_time + '12:00:00'
WHERE `start_time` < '11:00:00'
adds 12 seconds.

Try using ADDTIME instead of DATE_ADD. You could do SET start_time = ADDTIME(start_time, '12:00:00')

UPDATE `events`
SET start_time = start_time + INTERVAL 12 HOUR
WHERE `start_time` < '11:00:00'
The MySQL functions that accept INTERVAL arguments are mostly unnecessary; you can just add and subtract intervals with + and -.

set start_time = ADDTIME(start_time,'12:00:00')
DATE_ADD works fine with timestamp etc, but not with TIME

update my_table SET modified_date = ADDTIME(scheduled_date, '03:15:00')
This will add 3 hours , 15 minutes in modified_date

if the developer does not want to update data and wants to add hours or minutes to time. It can be done following way:
The developer can use an AddTime() function to add hours to time column in MySQL.
It can be used like below way:
AddTime(COLUMN,’01:00:00′) to add an hour in MySQL time column
AddTime(COLUMN,’00:01:00′) to add a minute in MySQL time column
sql query example:
select id,name,AddTime(login_time,'01:00:00') as login_time FROM `table`

First answer:
SET start_time = ADDTIME(start_time, '12:00:00')
Will only work if start_time is less than 12 hours.
If start_time is for example 13:00:00, then the end result will be 25:00:00, to get 01:00:00, you can use the following trick:
SET start_time = DATE_FORMAT(ADDTIME(CONCAT('1970-01-01 ', start_time), '12:00:00'), '%H:%i:%s')
(I used this to correct for the timezone)

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

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

Mysql to get the current date with time 23:59:59

If I execute
select current_date;
This will give the current date.
What if I have to get the current date with 23:59:59 , So that it will be end of the day.
In SQL Server I used
Select CAST(CONVERT(VARCHAR(10), GETDATE(), 110) + ' 23:59:59' AS
DATETIME;
One method is to add seconds:
select date_add(CURDATE(), interval 24*60*60 - 1 second)
Another method is addtime():
select addtime(CURDATE(), '23:59:59')
Adding interval of 1 day - 1 second] would only work if the datetime / timestamp in question has time set to 00:00:00 (that's the case of CURDATE() and CURRENT_DATE).
However, if you need to work with datetime / timestamp set to, for example, 2021-08-05 02:00:00, the aforementioned method would yield 2021-08-06 01:59:59.
In such case, you may use DATE_FORMAT:
SELECT expire_at, DATE_FORMAT(expire_at, "%Y-%m-%d 23:59:59")
FROM coupons;
(tested on MySQL 8.0.18)

How can I get the date difference of a timestamp

I am trying to create a query that will limit insertion into a table based on the last time the poster sent data to the table.
For example if you posted data to the table then you are locked out of the system for another 10 hours. Here is what I came up with so far. But I get nowhere with the actual results on the data. Any help?
SELECT DATE( `date` )
FROM tablename
WHERE DATE( CURDATE( ) ) < CURDATE( ) - INTERVAL 1002
DAY
LIMIT 0 , 30
This will return a single post from the last 10 hours, if it exists:
SELECT *
FROM tablename
WHERE `date` >= NOW() - INTERVAL 10 HOUR
LIMIT 1
I'm assuming date is declared as DATETIME, since actual DATE does not contain the time part and hence is only day-accurate.
If date is an integer UNIX timestamp, use this:
SELECT *
FROM tablename
WHERE `date` >= UNIX_TIMESTAMP(NOW() - INTERVAL 10 HOUR)
LIMIT 1
There are a number of ways you could do this. Perhaps if you have a user settings table you could simply add a "last_insert" field, and store the timestamp as an integer value- that would be a super simple way to do it- you could check the current timestamp vs user_settings.last_insert and voila!
I suppose you could use datetime too. Whatever floats the boat.
First of all, you need a DATETIME column and not a DATE column. Assuming that tablename.date is a DATETIME column, then 10 hours before right now is CURRENT_TIMESTAMP - INTERVAL 10 HOUR.
First of all create a Time (TIMESTAMP DEFAULT CURRENT_TIMESTAMP) columnt in your table. It will be automatically set to current date on row insert
Then check:
SELECT COUNT(*) FROM Table WHERE Time > NOW() - INTERVAL 10 HOUR
If its 1 or more - block
You must compare the time last post was put with current time, not current time with current time :|

MySQL query: get data in specific interval

I have a MySQL Table with one datetime column. I want to prevent that the PHP-script gets to much data. So i'm searching for a solution that a MySql query only selects rows which have a distance of 1 minute or whatever. is there something simple or do i have to code a for-loop with a new mysql query every time.
Example
timestamp
2012-09-25 00:00:00-->
2012-09-25 00:00:50
2012-09-25 00:01:23
2012-09-25 00:01:30-->
2012-09-25 00:02:33
2012-09-25 00:02:40
2012-09-25 00:03:01-->i want those
thanks in advance
Try this :
SELECT create_time
FROM timeTable
WHERE create_time
IN (
SELECT min( create_time )
FROM timeTable
GROUP BY FROM_UNIXTIME( UNIX_TIMESTAMP( create_time ) - MOD( UNIX_TIMESTAMP( create_time ) , 60 ) );
How it works :
i) Groups the table by datetime rounded to the interval, 1 minute (60 seconds) here.
ii) Gets the top row from each group.
This can be a good sampling criteria for your data.
This query can be optimized alot on these points:
i) Put a where clause for a date = REQUIRED DATE, and then do other operations on hour+minutes instead of whole datetime.
ii) If your interval is 1 minute, then substring of the timestamp or date_format can be tried too to round it off to nearest minute.
eg.
SELECT create_time
FROM timeTable
WHERE create_time
IN (
SELECT min( create_time )
FROM timeTable
GROUP BY DATE_FORMAT( `create_time` , 'Y-M-D %H:%i' )
);
Try this
SET #time := '1000-01-01 00:00:00';
SET #interval := 60;
SELECT colDate
FROM table
WHERE TIMESTAMPDIFF( SECOND, #time, colDate ) >= #interval
AND #time := colDate
How it works.
#interval is the time difference desired between the current and previous colDate. The first parameter in TIMESTAMPDIFF determines the unit of time that the interval will use. ex: SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.
#time keeps track of the previous colDate, and it is compared with the current row. If the difference between the previous and current colDate is equal to or greater than the interval, it is included.
WHERE timestamp LIKE '%:30:00%' will get you every 30 seconds..
But this will only work if you have uniform entries..if your timestamps dont all end evenly.. you'll need to let us know.
EDIT
I think you may be looking for this:
How do you select every n-th row from mysql