For example, I have a time format: 1000
how do I convert into MySQL Time() 10:00:00
It can also be more complex, since some numbers are 3 digits long, for example:
900 into MySQL Time() 09:00:00
Let me know if this needs more explaining.
You just want to use str_to_date()
cast your into to a char so its a string and pad it with lpad to keep a leading 0 :)
SELECT STR_TO_DATE(LPAD(CAST(my_col AS CHAR(25)), 4, '0'), '%H') -- or %k
or you can just drop the cast since mysql will do the converting for you
SELECT STR_TO_DATE(LPAD(my_col, 4, '0'), '%k')
FIDDLE
Use CONVERT() -- (http://dev.mysql.com/doc/refman/5.7/en/charset-convert.html)
You'll have to pad it with zeros to show CONVERT() that it is in hours and not minutes.
Specific to your example:
> SELECT CONVERT(CONCAT('1000','00'), TIME) AS time1;
time1
10:00:00
or
> SELECT CONVERT(CONCAT(`fieldname`,'00'), TIME) AS time1 FROM `tablename`;
time1
10:00:00
Select cast(number*100 as time)
Explanation: as mysql documentation on time data type says:
MySQL interprets abbreviated values without colons using the assumption that the two rightmost digits represent seconds (that is, as elapsed time rather than as time of day). For example, you might think of '1112' and 1112 as meaning '11:12:00' (12 minutes after 11 o'clock), but MySQL interprets them as '00:11:12' (11 minutes, 12 seconds). Similarly, '12' and 12 are interpreted as '00:00:12'.
So, if you want to interpret 1000 as 10 hours and 0 minutes, you need to multiply your number by 100 an interpret it as a time expression.
Related
I need to write a select query to sum the time interval from MySQL table where the time interval is stored as text and in the format similar to following
10 days 3:28:31
In the PostgreSQL query we can simply use ::interval and it converts above to interval and we can use Sum method over it in PostgreSQL query but I am unable to do the same in MySQL. Any help would be appreciated.
MySQL does not have an interval data type. It does use the interval keyword -- which is a bit confusing. But that is a syntactic element, rather than a data type.
One thing you can do is use the time data type. This supports times up to 838 hours -- or about 35 days. That is enough for many purposes.
Otherwise, the recommendation is to use a single time unit and to store the value as a numeric quantity. For instance, "5 days 10:20:00" would be:
5.43055555556 days (5 + 10 / 24 + 20 / (24*60))
130.333333333 hours
7820 minutes
I use the TIME field quite frequently to store a second-accurate duration. However, how would I store something like:
4 hours, 3 minutes, 1.1828999 second
I would prefer not to store it as a float or decimal, but in a way that is clear it's a time duration. How should I do this?
Store it as a TIME(6) to get up to six digits of precision for fractional seconds. Time precision is a feature introduced in MySQL 5.6.
Storage of fractional seconds costs an extra 3 bytes per column. See https://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html#data-types-storage-reqs-date-time for details.
Then you can format it as you select the value:
mysql> select current_time(6),
date_format(current_time(6), '%H hours, %i minutes, %s.%f seconds') as `formatted time`;
+-----------------+-----------------------------------------+
| current_time(6) | formatted time |
+-----------------+-----------------------------------------+
| 00:24:53.843700 | 00 hours, 24 minutes, 53.843700 seconds |
+-----------------+-----------------------------------------+
See DATE_FORMAT() for other formatting options.
I have a Database in 4th Dimension. It stores its time values in number format (seconds).
Now I have a Time duration of 29:30:00 ( 29 hours: 30 min : 00 Sec )
But this conflicts with the TIME rules of mySQL ( cant be > 23:59:59 )
I tried to convert this to number and save it, but I suspect it MySQL sees that it is a Time field and a number is not valid.
I was thinking about a format like DD:HH:MM:SS but im not sure if that is allowed.
There should be no problem storing time values greater than 24 hours in either 4D or mySQL.
4D:
A Time field, variable or expression can be in the range of 00:00:00 to 596,000:00:00
mySQL:
TIME values may range from '-838:59:59' to '838:59:59'.
I want to convert seconds to minute : seconds format in sql select statement.
At the moment I am using:
SELECT SEC_TO_TIME(duration) from messages;
It works perfectly but it gives me this format of time: hh:mm:ss
but I need mm:ss
Is it possible to convert seconds into mm:ss format using sql query?
If the value is less than an hour, then just do:
SELECT RIGHT(SEC_TO_TIME(duration), 5) from messages;
If you might go over an hour, then do the arithmetic:
SELECT CONCAT_WS(':', FLOOR(SEC_TO_TIME(duration) / 60),
SEC_TO_TIME(duration) % 60)
I recently had a similar project where I needed to convert stored seconds to m:ss format. No matter the amount, there needed to be at least one digit representing minutes and two digits representing seconds. The hours placeholder was forbidden, so the minutes value could acceptably go beyond 59 and also contain more than 2 digits. The minute value must not be zero-padded.
This is what I used: (SQLFiddle Demo)
CONCAT(FLOOR(seconds/60), ':', LPAD(MOD(seconds,60), 2, 0)) AS `m:ss`
aka
CONCAT(FLOOR(seconds/60), ':', LPAD(seconds%60, 2, 0)) AS `m:ss`
seconds | m:ss
-----------------
0 | 0:00
1 | 0:01
10 | 0:10
60 | 1:00
61 | 1:01
71 | 1:11
3599 | 59:59
3600 | 60:00
5999 | 99:59
6000 | 100:00
TIME_FORMAT(SEC_TO_TIME(seconds),'%i:%s') was unsuitable because the project specifications did not want the minute portion to be zero-padded. Here is a good post relating to this technique.
There is no single-digit minute option in TIME_FORMAT() or DATE_FORMAT().
If you are using MySQL 8.0+ you can use REGEXP_REPLACE like this to achieve a variable length string similar mickmackusa's answer:
REGEXP_REPLACE(SEC_TO_TIME(duration), '^(0|:)+', '')
I've created a stored procedure in MySQL Server 5.1.
How can I convert a timestamp to a string that represents the days, hours, and minutes difference between that timestamp and now?
For example, if the timestamp is 5 hours and 3 minutes ago I'll get '5 hours 3 minutes ago'.
select date_format(timediff(current_timestamp,last_timestamp),
'%k hours, %i minutes, %s seconds ago');
If you want more luxury you can do something like:
select concat
(
if(date_format(timediff(ts1,ts2)'%k')='0'
,'',date_format(timediff(ts1,ts2)'%k hours'),
if(date_format(timediff(ts1,ts2)'%i')='0'
,'',date_format(timediff(ts1,ts2)'%k minutes'),
if(date_format(timediff(ts1,ts2)'%s')='0'
,'',date_format(timediff(ts1,ts2)'%k seconds')
)
Plus a few extra spaces here and there.
If one of the timestamps is null naturally the result will also be null, you'll have to make sure it is not, or use ifnull(`timestamp`,now()) to fix that.
See: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
Have a look at the MySQL reference page for date and time functions at
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
Edit: Since I assume you are using Unix timestamps, the way to go is
FROM_UNIXTIME(timestamp, format)