Mysql datetime format add 10 minutes - mysql

Hi i have table with datetime variable.
I was wondering if i can somehow change the datetime column to add 1O minutes to stored date.
Perhaps some trigger has to be involved.
Thanks for help

I like the INTERVAL expr unit notation. It feels more readable to me:
SELECT NOW(),
NOW() + INTERVAL 10 MINUTE;
+--------------------------------+-------------------------------+
| NOW() | NOW() + INTERVAL 10 MINUTE |
+--------------------------------+-------------------------------+
| August, 12 2013 14:12:56+0000 | August, 12 2013 14:22:56+0000 |
+--------------------------------+-------------------------------+
If you want to select existing rows and add 10 minutes to the result:
SELECT the_date + INTERVAL 10 MINUTE FROM tbl;
If you want to alter existing rows stored in a table, you could use:
UPDATE tbl SET the_date = the_date + INTERVAL 10 MINUTE;
If you want increase by force a value by 10 minutes while inserting, you need a trigger:
CREATE TRIGGER ins_future_date BEFORE INSERT ON tbl
FOR EACH ROW
SET NEW.the_date = NEW.the_date + INTERVAL 10 MINUTE

add 10 minute in following way
SELECT ADDTIME(now(), '1000');

Related

SQL Select data in the last 1 minutes

i am currently practicing my SQL skills. I wanted to get all data in the past 1 minutes.
The query is SELECT * FROM menfesses WHERE created_at >= NOW() - INTERVAL 1 MINUTE;
But somehow, it returns all data.
I have also use date_add approach and nothing works
What did i do wrong? Thanks
Probably your server time that's not what you think it is.
This work with a 5 min laps.
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE t1
(`c_date` datetime)
;
INSERT INTO t1
(`c_date`)
VALUES
(NOW() - INTERVAL 30 MINUTE),
(NOW() - INTERVAL 2 MINUTE),
(NOW() - INTERVAL 1 MINUTE)
;
Query 1:
SELECT *,NOW() FROM t1 WHERE c_date >= NOW() - INTERVAL 5 MINUTE
Results:
| c_date | NOW() |
|----------------------|----------------------|
| 2023-02-09T15:44:05Z | 2023-02-09T15:46:20Z |
| 2023-02-09T15:45:05Z | 2023-02-09T15:46:20Z |
The last five minutes must be select with a BETWEEN.
Also testing you should add
SELECT created_at, Now() FROM menfesses WHERE created_at BETWEEN NOW() - INTERVAL 5 MINUTE AND NOW()
So that have a chance to debug it correctly
Between would only give you the correct data, as your test server could have dates later then now
Edit
a fiddle demonstrates my point https://dbfiddle.uk/c5Jlko65
You maybe off on the system you have

How can I set UNIX_TIMESTAMP correctly on my local?

I have a table which has a column named date_time. It is containing a unix number of the time. Something like this:
// mytable
+----+------------+
| id | date_time |
+----+------------+
| 1 | 1464499385 | -- 19 days ago
+----+------------+
-- ^ these are based on current time which is 1464566088
Also here is my query:
SELECT id,
(CASE WHEN FROM_UNIXTIME(date_time) >= CURDATE() THEN 'today'
WHEN FROM_UNIXTIME(date_time) >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) THEN 'yesteray'
WHEN FROM_UNIXTIME(date_time) >= DATE_SUB(CURDATE(), INTERVAL 7 DAY) THEN 'in last week'
ELSE 'in last month or more'
END) as `range`
FROM mytable
WHERE 1;
The result of query above on local isn't the same as on fiddle.
on local:
As you see the result on local is yesterday and on fiddle is today. Why there is a different and how can I fix it?
Note: when I select UNIX_TIMESTAMP on local and on fiddle, there is a different.
SELECT UNIX_TIMESTAMP(); -- 1464566511 (on local)
SELECT UNIX_TIMESTAMP(); -- 1464562972 (on fiddle)
So how can I set identically?
Sql fiddle server is in a different time zone.
Take 'UTC - your timezone' and subtract that many interval hours from your timestamp
You can convert with the difference using convert_tz :
SELECT CONVERT_TZ(datetime,'+00:00','-10:00');
Convert _TZ MySql documents
You can Google time in UTC and take the difference or you can set global tinezone to your local some support on that

Update timestamp values by subtracting 4 hours from each record [duplicate]

This question already has answers here:
How to subtract hours from a datetime in MySQL?
(3 answers)
Closed 8 years ago.
I have a created_at timestamp field. I need to update and subtract 4 hours from every record.
This is about updating the records, not changing the results of a query.
So 2014-08-20 18:00:00 would become 2014-08-20 14:00:00.
And 2014-08-21 03:00:00 would become 2014-08-20 23:00:00.
You can use a simple UPDATE statement:
UPDATE yourtable SET created_at = created_at - INTERVAL 4 HOUR;
This will update all rows accordingly to your needs:
From the documentation to DATE_ADD
Date arithmetic also can be performed using INTERVAL together with the
+ or - operator:
date + INTERVAL expr unit
date - INTERVAL expr unit
You can use INTERVAL to specify the time that needs to be subtracted:
UPDATE myTable
SET created_at = created_at - INTERVAL 4 HOUR
few ways you can do this.
you can set the date equal to another date that is interval'd 4 hours earlier like so
using adddate()
UPDATE table SET created_at = ADDDATE(created_at, INTERVAL -4 HOUR);
using date_sub
UPDATE table set created_at = DATE_SUB(created_at, INTERVAL 4 HOUR);
using just interval
UPDATE table SET created_at = (created_at - INTERVAL 4 HOUR);

select several averages of an interval

So my coworker is looking at a schema which could be described as something like this:
+--------------------+-----------+
| DATETIME timestamp | INT value |
+--------------------+-----------+
Every 5 minutes a row is entered with the value for that moment.
Here's where it gets tricky. He wants to get the average of every 8 hour period within a 7 day interval.
Certainly, I can think of solutions which involve some client side code, we were wondering if it was possible to do more in SQL.
So in essence, he wants:
SELECT timestamp, value
FROM table
WHERE timestamp >= NOW() - INTERVAL 7 DAYS
AND timestamp <= NOW();
And then breaking that up into 8 hour blocks, and averaging the contents of each block. (each block should have 12 rows, and there should be 3 blocks per day).
try
SELECT avg(`value`)
FROM `table`
WHERE timestamp >= NOW() - INTERVAL 7 DAY AND timestamp <= NOW()
group by concat(date(`timestamp`), case when hour(`timestamp`) between 0 and 7 then 1
when hour(`timestamp`) between 8 and 15 then 2
else 3 end)
If you are not tied by only doing it in your request, you could try the following method of splitting the intervals before doing the request :
boundary1 = NOW()
boundary2 = NOW()
FOR i = 0 to 21 //7 days, 3 intervals of 8 hours per days
boundary1 = boundary2
boundary2 = boundary1 - seconds(8 hours)
req = "SELECT timestamp, value FROM table WHERE timestamp >= "+boundary2+" AND timestamp <= "+boundary1
ENDFOR

How to convert UNIX time before 1970 to date format in MySQL?

I have a database using unix time for its dates ( i am using mySQL). I want to retrieve the dates in daily date format. This is my query:
SELECT FROM_UNIXTIME(time_created) FROM member
This works fine with dates after 1970 (for example, 1314162229) but doesn't work for dates before 1970 (for example, -769338000). Is there any work around here?
A possible workaround would be to have a constant handy corresponding to the seconds in a certain number of years (preferrably a multiple of 4). You could add this constant, translate the time and then subtract the number of years chosen.
Example: choose 40 years.
Determine the constant:
MySQL [files]> select adddate(from_unixtime(0), interval 40 year);
+---------------------------------------------+
| adddate(from_unixtime(0), interval 40 year) |
+---------------------------------------------+
| 2010-01-01 01:00:00 |
+---------------------------------------------+
1 row in set (0.09 sec)
MySQL [files]> select unix_timestamp(adddate(from_unixtime(0), interval 40 year));
+-------------------------------------------------------------+
| unix_timestamp(adddate(from_unixtime(0), interval 40 year)) |
+-------------------------------------------------------------+
| 1262304000 |
+-------------------------------------------------------------+
1 row in set (0.09 sec)
Now you can every unix timestamp x between 1930 and 20xx and use it.
select subdate(from_unixtime(x+1262304000), interval 40 year);
With your example -769338000, you get
MySQL [files]> select subdate(from_unixtime(-769338000+1262304000), interval 40 year);
+-----------------------------------------------------------------+
| subdate(from_unixtime(-769338000+1262304000), interval 40 year) |
+-----------------------------------------------------------------+
| 1945-08-15 17:00:00 |
+-----------------------------------------------------------------+
1 row in set (0.09 sec)
I found a new way:
converting to MySQL date:
SELECT DATE_ADD(FROM_UNIXTIME(0), interval YOURTIMESTAMPHERE second);
converting your epoch to a date string:
SELECT DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(0), interval YOURTIMESTAMPHERE second), '%Y-%m-%d');
And back
SELECT TIMESTAMPDIFF(second,FROM_UNIXTIME(0),'1960-01-01 00:00:00' );
source:
http://www.epochconverter.com/programming/mysql-from-unixtime.php#negavtiveEpoch
SELECT DATE_ADD(CAST('1970-01-01 00:00:00' AS DATETIME), INTERVAL `time_created` SECOND) FROM `member`
To my knowledge there is no such thing as UNIX time prior to 1/1/1970 00:00 UTC. More at Wikipedia.