MySQL. How to get o'clock time from hour - mysql

I have a time datatype field "MyTimeField" in MySQL. So for example the value can be 10:30:00
I want to select the "o'clock time" in time format, so the output will 10:00:00
tried
TIME_FORMAT(hour(MyTimeField),'%T')
but no luck, this returns 00:00:10
Any ideas?

If you want only the hour you could use
TIME_FORMAT(MyTimeField,'%H:00:00')

Related

split time between start time and end time with one hour interval in mysql

I have start time and end time with time stamp format. Now i want to know how to split time between two things with 60 minutes interval.
I have booking table. so now split the available slots with one hour interval for each date
I have started ,endedslots for booking. For example started is2014-05-02 18:00:00and ended is2014-05-02 22:00:00`. Now I want like this:
START TIME
-------------------
2014-05-02 18:00:00
2014-05-02 19:00:00
2014-05-02 20:00:00
2014-05-02 21:00:00
Try this
SELECT `START_TIME`
FROM TABLE
WHERE `START_TIME` BETWEEN "2014-05-02 18:00:00" AND "2014-05-02 22:00:00"

Date Difference in MySQL in Minutes

I need to find the time difference in Hours for the following Dates in MySQL - Can i use Datediff functions?
2014-01-01 07:27:21 and 2014-02-01 11:29:00
I tried using DATEDIFF(MINUTE,'2014-01-01 07:27:21','2014-01-01 11:29:00') but apparently MySQL is giving an error.
Time difference in minutes:
SELECT ROUND(TIME_TO_SEC(timediff(date1,date2))/60) AS diff
Example:
SELECT ROUND(TIME_TO_SEC(timediff('2014-01-01 11:29:00','2014-01-01 07:27:21'))/60) AS diff
Result:
242
Time difference in hours:
SELECT ROUND(TIME_TO_SEC(timediff(date1,date2))/60/60) AS diff
if you need number of hours with fractions then remove ROUND.

Query Time range between Dates using DATETIME mysql

I have a database table that has fields as such :
TIME(Datetime) Update_ID
2013-11-25 05:00:14 XC3
2013-11-25 06:00:13 XC4
2013-11-25 06:00:19 XC5
2013-12-25 23:00:14 XC6
2013-12-25 24:00:00 XC7
So assuming i want to find a trend on the updates to know which period of the day has the a particular number of updates, what i initially think of is doing something like this :
SELECT COUNT(TIME) FROM table WHERE TIME between '06:00:00' and '12:00:00'
But this doesn't work because i think since the date is not added with the time, a default value for date is added(some date around 1970). If, i add the beginning and enddate in my query, i am afraid it won't give me the results i need.
Use
WHERE HOUR(TIME)...GROUP BY DAY(TIME)
in case you have more than 1 day
You are correct, the problem is that when you do not specify the date, a default one is added.
You can use the EXTRACT function to extract the time from a date, like this:
SELECT COUNT(*)
FROM mytable
WHERE EXTRACT(HOUR_SECOND from TIME) between 60000 and 120000
Note that the time portion in the condition is specified in a different format - i.e. as numbers, without colons and quotes.
Demo on SqlFiddle.

MySQL custom datetime

I am trying to display data associated with date. However in my case, I don't want the date to start at 00:00:00 and finishes at 23:59:59. Rather I want it to start at 16:00:00 and finishes at 06:00:00 the next day. In other words I want to create a custom time for date.
In the same time I want to GROUP_BY date.
For instance I have these values in the database:
want it to give me:
date: 2013-09-08 count: 2
date: 2013-09-09 count: 1
I am not asking for code, but a way to think about it, or useful methods.
Thank you in advance!
The simplest method is to take the existing date and subtract six hours to get the "effective" date. You would do this for output purposes only.
Example:
select date(datecol - interval 6 hour) as MyDate, count(*)
from t
group by date(datecol - interval 6 hour);
You can use a where clause to remove the times between 6:00 and 16:00 (unless that is a typo).

How do I convert negative time value into 24 hour time in MYSQL?

I have a column in a table of type TIME. I want to get a result that applies a time shift that results in a 24 hour clock representation of that shift. To add the shift, my query contains...
select addtime(boo,'01:00:00') as yada
But any value that gets taken out of the 24 hour range ends up outside the 24 hour range, such as...
23:45 ends up as 24:45 (when I want 00:45:00)
If I go the other way and subtract the hour from a value less than 1am, I get...
00:15 ends up as -00:45:00 when I want (23:15:00)
Now, I understand that the TIME format in MYSQL is "duration" and not the actual "time", but for the life of me I can't figure out how to convert to an actual clock time as outlined above. Please help me or kill me. Either will end my suffering.
A simple solution would be to just use a DATETIME data type instead, then ignore the date part. If you're not dealing with huge amounts of data, or searching by the actual times I can't see an issue.
As a bonus you'll be able to manipulate the data with the likes of + INTERVAL 1 HOUR etc.
When extracting it just use TIME(boo)
As you know, the MySQL TIME type is not restricted to 24 hour time so this is probably the closest you'll get... I'm sure you could construct a query using MOD() etc but it's probably not worth it.
A possible solution is just add your boo TIME value to any date (e.g. today) then add your time delta and after that just return time part with TIME()
SELECT TIME(CURDATE()
+ INTERVAL TIME_TO_SEC('23:45:00') SECOND
+ INTERVAL 1 HOUR) new_time
Output:
+----------+
| new_time |
+----------+
| 00:45:00 |
+----------+
Here is SQLFiddle demo