mysql datetime problem "Incorrect datetime value" and daylight savings - mysql

I have a problem storing a date/time. i.e.
DBD::mysql::st execute failed: Incorrect datetime value: '2022-03-27 01:00:00' for column smets2.electricity.time
Now the only possible thing I can see wrong with this is that perhaps this time does not exist because of daylight savings. 01:00:00 became 02:00:00
Here's the SQL I used to create the table
create table if not exists electricity
(time timestamp null,
consumption double);
So my question are ...
does mysql understand about daylight savings by default ?
What is the correct date/time for this time. Clearly it cannot be '2022-03-27 01:00:00', should it be '2022-03-27 02:00:00'
how can I force mysql to use UTC for date/time
Thanks

OK.
It seems that the problem is that I defined the field a 'timestamp' when it should have been 'datetime'.
'timestamp' fields get the timezone applied on input/output.
Won't make that mistake again :-)

Related

Subtract 5 hours from a datetime value that is type timestamp with timezone

I am querying a table that has a datetime column and the value is in the format time stamp with time zone. I've tried doing a select hour(timestamp,-5) as NTime and different variants of that, but the furthest I get is an error stating the timestamp is not a valid name/type. I'm officially going off the deep end on this....
Basically, I just need the new alias column values to be a time stamp that is 5 hours behind the timestamp that is in the original table. Thank you all in advance!!
MariaDB / MySQL doesn't have a "timestamp with timezone" data type.
DATETIMEs are simple wall time, and TIMESTAMPs are UNIX time_t timestamps (the number of seconds since 1970-01-01T00:00:00UTC).
You can convert DATETIME values from one time zone to another by with tz_convert().
SELECT tz_convert('2022-04-08 21:53', 'America/Chicago', 'UTC')
for example.
Or, just to do date arithmetic you can do stuff like this.
SELECT '2022-04-08 21:53' - INTERVAL 5 HOUR

Error Code: 1292 Truncated incorrect datetime value

insert into test_date
select
shopify_update
from `vw_shopify_json_price`
where cache_id=669
Error Code: 1292
Truncated incorrect datetime value: '2019-05-17T11:34:30-04:00'
CREATE TABLE `test_date` (
`t` datetime DEFAULT NULL
)
datetime does not allow a timezone or timezone offset. Some reasonable choices:
convert the timestamp to UTC and store that (my recommendation)
convert the timestamp to the timezone of your database and store that
I had a similar issue, which was caused by the date time field being stored as varchar.
The issue was resolved by doing this:
1) Taking first 10 characters of the timestamp
2) Converting it to date.
where str_to_date(left(my_future_date,10), '%Y-%m-%d') > now()
P.S. this is a hacky way, but i did not want to go through all the code related to this field, and doing all the changes. This solved my issue in 30 seconds. But solution took half an hour to find.

What's wrong with '2018-03-22 00:00:00" in MySQL?

I want to update a date field and set it to 2018-03-22 00:00:00 but I get the following stupid error:
Error Code: 1292. Incorrect datetime value: '2018-03-22 00:00:00' for column 'Date' at row 158917
This is the query I use for updating:
update assets.transactions
set date = date_add(date, interval 1 hour)
where date between '2018-03-21 23:00:00' and '2018-06-29 23:59:59';
What is wrong? I searched a lot and found out dates before 1970-01-01 00:00:01 are not supported by MySQL, that is acceptable, but a date in the middle of 2018? That's something I can't digest.
Is there any solution to make this work right?
I guess you're updating a TIMESTAMP column. I also guess you have your MySQL instance set to a timezone with a daylight time switchover on 23-March-2018. I guess the rules of your timezone switchover in your country mean that the clock rolls over from 21-March-2018 11:59:59 to 22-March-2018 01:00:00.
So the value 2018-03-22 00:00:00 just doesn't exist.
Strange, isn't it?
Try issuing this MySQL command, to set the time zone to UTC, before doing these sorts of mass timestamp updates.
SET time_zone = 'UTC';
Don't forget to switch it back before doing other operations. Or just do those operations from a different MySQL connection.

What difference between the DATE, TIME, DATETIME, and TIMESTAMP Types

I need to store both time and date in the mysql. So I used of NOW() function for that. But I don't know what should I use for type column im phpmyadmin. It should be noted that NOW() returns both time and date like this:
2014-11-11 12:45:34
Here is a solution, I can use of a separator for separating date and time (2014-11-11 and 12:45:34) and then store them in the DATE type and TIME type individually. Or I can use of VARCHAR type for storing both of them in one column. But I think these ways are not standard. what is standard type for storing both date and time ?
Here is my query: (also I don't know why NOW() function does not works)
INSERT INTO table (timedate) VALUES (NOW())
DATE: It is used for values with a date part but no time part. MySQL retrieves and displays DATE values in YYYY-MM-DD format. The supported range is 1000-01-01 to 9999-12-31.
DATETIME: It is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in YYYY-MM-DD HH:MM:SS format. The supported range is 1000-01-01 00:00:00 to 9999-12-31 23:59:59.
TIMESTAMP: It is also used for values that contain both date and time parts, and includes the time zone. TIMESTAMP has a range of 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC.
TIME: Its values are in HH:MM:SS format (or HHH:MM:SS format for large hours values). TIME values may range from -838:59:59 to 838:59:59. The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).
I have a slightly different perspective on the difference between a DATETIME and a TIMESTAMP. A DATETIME stores a literal value of a date and time with no reference to any particular timezone. So, I can set a DATETIME column to a value such as '2019-01-16 12:15:00' to indicate precisely when my last birthday occurred. Was this Eastern Standard Time? Pacific Standard Time? Who knows? Where the current session time zone of the server comes into play occurs when you set a DATETIME column to some value such as NOW(). The value stored will be the current date and time using the current session time zone in effect. But once a DATETIME column has been set, it will display the same regardless of what the current session time zone is.
A TIMESTAMP column on the other hand takes the '2019-01-16 12:15:00' value you are setting into it and interprets it in the current session time zone to compute an internal representation relative to 1/1/1970 00:00:00 UTC. When the column is displayed, it will be converted back for display based on whatever the current session time zone is. It's a useful fiction to think of a TIMESTAMP as taking the value you are setting and converting it from the current session time zone to UTC for storing and then converting it back to the current session time zone for displaying.
If my server is in San Francisco but I am running an event in New York that starts on 9/1/1029 at 20:00, I would use a TIMESTAMP column for holding the start time, set the session time zone to 'America/New York' and set the start time to '2009-09-01 20:00:00'. If I want to know whether the event has occurred or not, regardless of the current session time zone setting I can compare the start time with NOW(). Of course, for displaying in a meaningful way to a perspective customer, I would need to set the correct session time zone. If I did not need to do time comparisons, then I would probably be better off just using a DATETIME column, which will display correctly (with an implied EST time zone) regardless of what the current session time zone is.
TIMESTAMP LIMITATION
The TIMESTAMP type has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC and so it may not usable for your particular application. In that case you will have to use a DATETIME type. You will, of course, always have to be concerned that the current session time zone is set properly whenever you are using this type with date functions such as NOW().
Saty described the differences between them. For your practice, you can use datetime in order to keep the output of NOW().
For example:
CREATE TABLE Orders
(
OrderId int NOT NULL,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (OrderId)
)
You can read more at w3schools.
In shorter explanation
DATE: The DATE stores a date value in the form YYYY-MM-DD (year-month-day). It does not store time.
TIME: The TIME stores a time value in the form HH:MM:SS (hours-minutes-seconds). It does not store the date.
DATETIME: The DATETIME stores a date and time value in the form YYYY-MM-DD HH:MM:SS. It stores both the date and time.
TIMESTAMP: The TIMESTAMP is similar to the DATETIME, but includes a timezone. (Example of values YYYY-MM-DD HH:MM:SS +HH:MM, YYYY-MM-DD HH:MM:SS -HH:MM. +HH:MM and -HH:MM indicate the time zone from UTC (Coordinated Universal Time).

How Insert DateTime Vales into Mysql Databse …Only End User gives Date

I am new developer in .net,I Have requirement..like this ,when user pick date from date picker not time only date he/she pick up,then click insert that time ,i want insert that date and time is into Column exist with name "EnterdDate" data type is "DATETIME". by default 00:00:00 is stored in the Time format I don't want to be stroed that values I want store The at the Time insertion MySql Server Time.
ex:user 12/03/2013 ->insert->click presently assume server time is 13:00:00
i want insert This Date value --> 2013-03-12 13:00:00 ok for me.
after 10 min I want insert the another record at time i want 2013-03-12 13:10:00
like server time in place of default time i am needed
*i don't need the DateAndTime like is :2013-03-12 00:00:00 not Ok for me.
please give best answer the above question.**
Use ADDTIME() to add the CURTIME() to the given date literal:
INSERT INTO my_table
(EnteredDate)
VALUES
(ADDTIME(CAST('2013-03-12' AS DATETIME), CURTIME())
One needs to CAST() the literal to a DATETIME value because ADDTIME() does not work with DATE types.