Reputation: 29495
I'd like to create a mysql table that stores time (hours and minutes), such as 456:45 (456 hours and 45 minutes)
CREATE TABLE mytable(id VARCHAR(40) NOT NULL, my_time DATE NOT NULL);
which data type should be used?
Upvotes: 1
Views: 3279
Reputation: 57453
If the TIME datatype range is enough for you, go for it. It stores information with a higher precision than you probably need, and has some quirks:
out-of-range TIME values are clipped to the appropriate endpoint of the TIME range.
Otherwise, if you only need minutes, you can employ a DECIMAL datatype. You have two approaches available:
Advantages: you have full precision with two digits, and you can immediately read the minutes by truncating.
Disadvantages: 0.30 + 0.30 ought to give 1.00, not 0.60. Also, "One minute before 27:00" will be 26.99, not 26.59.
Advantages: calculations become very simple.
Disadvantages: it's not immediate to look at 17.25 and interpret it as 17 hours and 15 minutes. You need to get the decimal part and multiply by 0.60. Also, to have acceptable precision, you need more than two decimals (3 or 4 depending on conversion techniques).
A further possibility is to do what MySQL does (albeit at the microsecond level) and use an INTEGER field to store the minutes. Then 1200 hours 30 minutes will be represented by the integer 72030. Divide by 60 to get the hours, do a modulo 60 to get the minutes.
Upvotes: 2
Reputation: 3481
TIME
- displays values in HH:MM:SS
The range is '-838:59:59' to '838:59:59'.
You can assign values to time column using strings or numbers.
Upvotes: 2
Reputation: 158
The data type that should be used is:
DATETIME
OR take a look at this:
http://www.w3schools.com/sql/sql_dates.asp
Hope this helps,
Sohail.
Upvotes: 0
Reputation: 6661
1) Create VARCHAR
column and when you save data use separator like :-
456:45,456_45,456-45
2) Other better option is convert time in second
or Create Int
column and save time in seconds
Upvotes: 0