What's wrong?
A new column (in Excel) was made for calculating Duration
Using formula to subtract values from 2 cells
For example: 2021-01-01 07:30 - 2021-01-01 07:00; Duration = 00:30:00
In MySQL table, I set the data type for the column as TIME, but it would return with error when importing
Error message: 'Error Code: 1292. Incorrect datetime value: '00:13:00 ' for column 'ride_length'
What I have tried:
Changing data type to DATETIME, INT, TIME -> None worked
Changing the data format to HH:MM:SS, HHMMSS, 'YYYY-MM-dd HH:MM:SS' in CSV -> None worked
Question
What kind of data type I should set in MySQL?
If it was the CSV file's problem, what kind of 'data format' I need to set?
Thank you very much
Actually, it might be easier for you to just import the two datetime values and then use TIMSTAMPDIFF inside MySQL:
SELECT TIMESTAMPDIFF(MINUTE, '2021-01-01 07:00:00', '2021-01-01 07:30:00')
-- 30
Note that because the difference is actually a computed quantity, it generally makes more sense to compute it when you select. This way, should one of the two values be updated later, you don't have to worry about maintaining the difference column.
Related
I am using MySQL 8 and have a problem with this type of query:
INSERT INTO review (name, create_date) VALUES('name', CONVERT(timestamp, DATETIME) - 1)
I have not had this error when using this expression in a where clause.
When the value for the timestamp is like '2020-12-16 06:15:01' it's working.
But with a value of 0 seconds (like: '2020-12-16 06:15:00') an error is dropped.
Incorrect datetime value: '20201216061499' for column 'create_date' at row 1
code: ER_TRUNCATED_WRONG_VALUE
errno: 1292
sqlState: 22007
I used this type of expression in my whole project. Is there a simple solution to this problem, without changing each expression?
Is that one a bug?
One solution to this problem is:
DATE_SUB(CONVERT(timestamp,DATETIME) INTERVAL 1 SECOND).
But as I already mention this requires changing each expression.
You do need to update each expression. When you subtract a number from your timestamp, it first converts your timestamp into a number (e.g. 20201216061500), then you are subtracting one and, because the column you are inserting is a datetime, it tries to interpret the resulting number as a date/time, failing when the subtraction produced 20201216061499. The correct way to subtract one second is to say - INTERVAL 1 SECOND or use DATE_SUB(..., INTERVAL 1 SECOND).
My data contains hour & minute of time and I want to load this data into MySql database.
Sample Data: 305 -- This means Hours=03 & Minutes=05
But when I upload the data into MySql column of type TIME, it is stored as 00:03:05 (HH:MM:SS).
Whereas, I want it to be stored as 03:05:00.
I can make changes to my data using Python & then load into MySql. However, I was wondering if there is a way to do it using MySql itself.
It is not up to MySQL to guess what you mean by "305". It's up to you to convert/format yor data to "03:05:00", then MySQL will undertand it properly.
insert into try (elasp) values ("03:05:00")
See MySQL documentation: https://dev.mysql.com/doc/refman/8.0/en/time.html
While converting to TIME datatype the numeric value is assumed to have HHMMSS format.
MySQL 8.0 Reference Manual / ... / The TIME Type:
Be careful about assigning abbreviated values to a TIME column. MySQL interprets abbreviated TIME values with colons as time of the day. That is, '11:12' means '11:12:00', not '00:11:12'. 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'.
If you want to use HHMM format during convertion then simply multiply the value by 100.
CREATE TABLE test (int_source INT, time_destination TIME);
INSERT INTO test (int_source) VALUES ('305');
SELECT * FROM test;
UPDATE test SET time_destination = int_source * 100;
SELECT * FROM test;
✓
✓
int_source | time_destination
---------: | :---------------
305 | null
✓
int_source | time_destination
---------: | :---------------
305 | 03:05:00
db<>fiddle here
Or, if the value to be converted to TIME datatype has some string type you may concatenate '00' to it:
SET time_destination = CONCAT(int_source, '00')
I have created a MySql table and feed data therein with below code
CREATE TABLE IF NOT EXISTS DB (
INN VARCHAR(200) NOT NULL,
Time BIGINT not NULL
);
Now I want to get a table which will report the Maximum and Minimum values of Time when grouped by INN. Below is my code -
SELECT INN, from_unixtime(MIN(Time)), from_unixtime(MAX(Time)) FROM DB GROUP BY INN
I want to get the Time values reported as normal date-time instead of Epoch time. But with above code, I am getting <NA> values.
A typical Time value is like 1546380001264082944
Can someone please help me to get the correct code to achieve the same.
The problem here is to do with the precision of the unix timestamp you are using.
Consider this:
SELECT FROM_UNIXTIME(1546380001), FROM_UNIXTIME(1546380001264082944)
The output is:
2019-01-01T22:00:01Z (null)
The timestampt value you have, 1546380001264082944, contains a level of precision beyond that accepted by MySQL.
The definition of FROM_UNIXTIME is:
FROM_UNIXTIME(unix_timestamp[,format])
The doc states:
unix_timestamp is an internal timestamp value representing seconds
since '1970-01-01 00:00:00' UTC
The precision of your timestamp is significantly greater than seconds since the Unix Epoch.
The docs are available here.
The value 1546380001264082944 is too big to be epoch seconds or even milliseconds. This is easily verified by putting this value on https://currentmillis.com/.
You have stored precision upto a nanosecond. So, divide the column value by 1e9 before passing them to from_unixtime.
SELECT INN, from_unixtime(MIN(Time) / 1000000000), from_unixtime(MAX(Time) / 1000000000)
FROM DB
GROUP BY INN
I Want to create a table in MySql for which has a column for Time
Value for time column must accept like 9AM to 10PM
+-----------------------------+
| DATE |
+-----------------------------+
| 9AM to 10PM |
+-----------------------------+
Which datatype should i use ?
If i don't want to use varcar & If i can use TIME data-type- How to represent values there ?
I am looking for create table and insert statement as an answer for above specifications ?
I am a newbie so please go easy with answers
Thanks !
As suggested above in comment the table structure would be:
Create Table YourTable
(
FromTime Time,
ToTime Time
)
Now Insert values as below:
Insert into yourTable
VALUES(TIME(STR_TO_DATE('09:00 AM', '%h:%i %p')),TIME(STR_TO_DATE('10:00 PM', '%h:%i %p')))
Use Time Data Type to save Time.
You can not insert 9AM to 10PM in datetime datatype column.
you have to use datatype whose representation should be like string, like varchar.
It's hard to tell exactly what you need based on what you've described - if you could provide a little more context we might be able to offer you more insight?
But as #rkp has mentioned, if you want the time interval to be represented as "9AM to 10PM", you will have to use some form of string/text field e.g. varchar
Alternatively, depending on how you plan to use this data, you could also use two fields - one for start time and one for end time. It would probably then be recommended that you use a datetime field since this will allow for much more flexibility (e.g. when calculating differences between time periods etc)
For reference - these are your options:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html
SQL beginner here !
What is the most convenient way to create datetime objects within an SQL function, especially generating a datetime object for a given day, month, and year?
Thanks !
Everything you want to know about MySQL datetime functions is right here. Well, probably most everything.
In MySQL
CAST('YYYY-MM-DDThh:mm:ss:uuuuuu' AS datetime)
where:
YYYY: Year
MM : Month
DD : Day
hh : Hour
mm : Minutes
ss : Seconds
uuuuuu : Microseconds
EDIT: I changed from mmm (miliseconds) to uuuuuu (microseconds) since MySQL suports 6 digits
Given a datetime column: INSERT INTO Table ('datetime_column') VALUES (CURDATE( ))
Given a timestamp: INSERT INTO Table ('timestamp_column') VALUES (CURTIME())
Using unix time: INSERT INTO Table ('timestamp_column') VALUES (UNIX_TIMESTAMP())