Error Code: 1292 Truncated incorrect datetime value - mysql

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.

Related

Datetime 0000-00-00 00:00:00 using CURRENT_TIMESTAMP

I'm running xampp 3.3.0, mysql 8.0. In my database, there is a column datetime with type "datetime". I'm trying to insert data in it with CURRENT_TIMESTAP.
Example of query:
INSERT INTO `statistics` (`chat_id`, `user_id`, `message_id`, `datetime`, `ai_unique`) VALUES ('988', '767', '98765', 'CURRENT_TIMESTAMP', NULL);
It inserts, but I'm getting
Warning: #1265 Data truncated for column 'datetime' at row 1
And instead of current timestamp, it inserts 0000-00-00 00:00:00
Some time ago it worked just fine, but now it's inserting 0000-00-00 00:00:00. If I manually write a date here like 2023-01-11 22:52:01 it works just fine
'CURRENT_TIMESTAMP' (with single-quotes) is a string. It doesn't have a datetime value. MySQL makes an effort to convert the string to a datetime, just as it would convert the string '2023-01-24 13:09:00' to a datetime. But the string you used cannot be converted in this way.
CURRENT_TIMESTAMP (a pseudoclumn with no single-quotes) has a datetime value.
CURRENT_TIMESTAMP() (a function) also returns a datetime value.

mysql datetime problem "Incorrect datetime value" and daylight savings

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 :-)

datetime error in generated columns in MySQL

I have a table 'tbl1' in which there are two fields:
created_at of type datetime and NULL is allowed.
num_days of type INT(11) and NULL is allowed and default value is 3.
I am trying to create a generated column 'cut_off' of type date and column of STORED type.
I am using this command:
alter table tbl1 add column cut_off date GENERATED ALWAYS AS (DATE(created_at + num_days)) STORED;
I am getting this error:
ERROR 1292 (22007): Incorrect datetime value: '20181119063562'
But in this query:
select distinct(DATE(created_at + num_days)) from tbl1;
is running fine and giving no errors.
Any help would be highly appreciated.
you are adding seconds not days, and a minute cannot have 62 seconds
perhaps try
DATE(created_at + INTERVAL num_days DAY)

How do I load a MySQL timestamp field into a specific format from a flat file?

I have some data in a flat file that includes a timestamp field that looks like 1900-01-01 00:00:00. I've been trying to load it into a MySQL table but MySQL is taking it and setting it as 0000-00-00 00:00:00 which is causing issues for me. I've tried running some update commands on the field, but it stays as 0000-00-00 00:00:00.
I know of the from_unixtime function, but how would I use that here? This is the command I'm running to load the data:
load data local infile 'file.txt' into table tgt_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\'' (tbl_name, db_name, timestamp_field, other_col);
The field in question here is timestamp_field.
I've managed to use from_unixtime like update tgt_table set timestamp_field=FROM_UNIXTIME('1900-01-01 00:00:00') where tbl_name = 'test'; but timestamp_field ended up looking like: 1969-12-31 18:31:40
Would appreciate any help on how to format either the column or the SQL to get this inserted correctly
Looks like it might be a column type issue.
From the MySQL 5.6 documentation on time/date fields:
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
You used the 1900-01-01 in your example which is outside the allowable date range for timestamp. Instead, try using a DATETIME field which has a larger allowable range.
The DATETIME type 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'.

Error Code: 1292 Truncated incorrect CHAR(8) value: '20160331000000'

i am trying to get a date from subtracting interval of 1 day from a date
and stored concating this with P in a variable temp_new_date_name that's datatype is varchar(256)
in stored procedure.
and new_date is datetime datatype.
During debug i found that new_Date value is '2016-04-01 00:00:00'
set temp_new_date_name =concat('p',CAST(((new_date - INTERVAL 1 DAY)+0) as char(8)));
Then i got following error
Error Code: 1292 Truncated incorrect CHAR(8) value: '20160331000000'
while i have tried following on my local,Then its working fine
select concat('p', CAST((('2016-04-01 00:00:00' - INTERVAL 1 DAY)+0) as char(8)));
i have tried google ,but didn't find any working solution.
Your help is appreciable.
Thanks
Let me know if you want more detail regarding this question
I resolved this answer by increasing the size of char(8) to char(20).