Actually I have time and exp field in mysql database.
I am successfully inserting current time in time field using : CURTIME();
But i also want to insert exp time in exp filed ie: current time + 2 hrs..
It should show :
time : 4:00:12AM
exp : 6:00:12AM
You could use addtime() for this.
So for your example:
SELECT ADDTIME(curtime()', '02:00:00.0000')
should work.
You can try this:-
SELECT ADDTIME(CURTIME(),'02:00:00.000000');
Hope This Will Help You.
Use DATE_ADD function to specify the interval which is 2 hours in your case. DATE_ADD(CUR_TIME, INTERVAL 2 HOUR) will work fine.
Here is the great documentation for date time functions -
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
Use following format:
INSERT INTO table1
(field1, field2,....,cur_time_field,exp_field,...fieldN)
VALUES
(value1,value2,....,NOW(),DATE_ADD(NOW(), INTERVAL 2 HOUR),...fieldN)
Update:
INSERT INTO table1
(field1, field2,....,cur_time_field,exp_field,...fieldN)
VALUES
(value1,value2,....,NOW(),DATE_ADD(CURTIME(), INTERVAL 2 HOUR),...fieldN)
Related
I'm after a mysql trigger that will set the column peak to 0 or 1 based on the timestamp hour between 08:01 & 23:59.
Can anyone help?
example:-
userstats.timestamp = 2018-07-01 12:27:20
peak = 1
One option is to use the DATE_FORMAT function to isolate the time component of your timestamp, e.g.
SELECT *
FROM userstats
WHERE DATE_FORMAT(timestamp, '%H:%i') BETWEEN '08:01' AND '23:59';
Your actual query may not look exactly like this, but this would seem to answer the crux of your question.
How to caculate sum of times of my colonne called "timeSpent" having this format: HH:mm
in SQL? I am using MySQL.
the type of my column is Time.
it has this structure
TimeFrom like 10:00:00 12:00:00 02:00:00
TimeUntil 08:00:00 09:15:00 01:15:00
Time spent
total time 03:15:00
SELECT SEC_TO_TIME( SUM( TIME_TO_SEC( `timeSpent` ) ) ) AS timeSum
FROM YourTableName
100% working code to get sum of time out of MYSQL Database:
SELECT
SEC_TO_TIME( SUM(time_to_sec(`db`.`tablename`)))
As timeSum
FROM
`tablename`
Try and confirm.
Thanks.
In MySQL, you would do something like this to get the time interval:
SELECT TIMEDIFF('08:00:00', '10:00:00');
Then to add the time intervals, you would do:
SELECT ADDTIME('01:00:00', '01:30:00');
Unfortunately, you're not storing dates or using 24-hour time, so these calculations would end up incorrect since your TimeUntil is actually lower than your TimeFrom.
Another approach would be (assuming you sort out the above issue) to store the time intervals as seconds using TIMESTAMPDIFF():
UPDATE my_table SET time_spent=TIMESTAMPDIFF(start, end));
SELECT SEC_TO_TIME(SUM(time_spent)) FROM my_table;
If the data type of the timeSpent column is TIME you should be able to use the following query:
SELECT SUM(timeSpent)
FROM YourTableName -- replace YourTableName with the actual table name
However, if that is not the case, you may have to use a cast to convert to the Time data type. Something like this should work:
SELECT SUM(timeSpent - CAST('0:0:0' as TIME))
FROM YourTableName -- replace YourTableName with the actual table name
I have timestamp values in my db. It has values like 2014-11-25 10:30:00.
I need to get all records between two dates and that has time between certain range like between 2014-10-20 to 2014-11-25 and between 9am to 7pm..
I need the query for this...
You can use the following query , I used it in my code for displaying data between two dates.
SELECT * from tablename WHERE columnname BETWEEN '2014-10-20 00:00:00' AND '2014-11-25 23:59:59'
The query includes start time of the particular date to end time of ending particular date.
You edit your query according to your start and end timings.
You can use internal mysql functions for convert datetype.
I think you need DATE() and TIME() functions.
Details you can find here
Thanks for your reply guys. I have found the answer
SELECT * FROM alerts
WHERE DATE BETWEEN '2014-11-16' AND '2014-11-26'
AND TIME(DATE) BETWEEN '09:00' AND '19:00'
Is giving the expected result.. :-)
I'm wondering how can I count two sets of time and then the value would like in a stopwatch format (00:00.00)
For example if the time is
05:00:00 to 10:53:34
How can I count the time?
Thank You
You may wish to look at TIMEDIFF(stoptime, starttime) and other time functions.
TIMEDIFF('22:53:34','05:00:00')
might do it.
This does too.
TIMESTAMPDIFF(MINUTE, '22:53:34','05:00:00')
with the function TIMEDIFF:
SELECT TIMEDIFF('10:53:34','05:00:00')
DEMO
Please note that the output of the demo is formatted by the fiddle and not the same as if used directly.
If you want fractional seconds, I can't show you this in a fiddle, but this should work in MySQL 5.6.16:
You've got to adapt your CREATE TABLE statement and use the %f specifier for fractional seconds.
create table example12(
starttime time(6),
stoptime time(6)
);
INSERT INTO example12 (stoptime, starttime) VALUES
(
TIME(STR_TO_DATE('10:53:34.530', '%H:%i:%s.%f')),
TIME(STR_TO_DATE('05:00:00.000', '%H:%i:%s.%f'))
);
SELECT *, DATE_FORMAT(
TIMEDIFF(stoptime, starttime),
'%H:%i:%s.%f'
)
FROM example12;
In joomla, it has jos_session table in which time is stored as unixtimestamp in 'time' column.
I want to select the records which are older then a week or two days, basically any interval.
Not much hands on DB, but i did tried with date_sub, but it seems taking date as an argument. So I also tried using FROM_UNIXTIME to convert, but nothing seems to be working.
The last query I tried was
SELECT username FROM jos_session WHERE DATE_SUB(FROM_UNIXTIME(time,'Y-m-d'), INTERVAL 2 DAY );
But it seems to giving empty set and many warnings!
Can anyone please help!
Thanks in advance,
Tanmay
Try this. It should work:
SELECT
username
FROM
jos_session
WHERE
TO_DAYS(FROM_UNIXTIME(CAST(timeAS UNSIGNED))) < TO_DAYS(FROM_UNIXTIME(UNIX_TIMESTAMP()))-2