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)
Related
I have a column called "month" that I have imported into MySQL:
Datatype: TEXT
Display: 2021-01
My goal is to convert this column into some sort of DATE datatype (ie. DATE, DATETIME) so that MySQL will recognize it as yyyy-mm.
I have tried the following method but still received NULL
--- (1) add dummy date
SELECT CONCAT(month, '-01')
FROM tablename;
--- (2) convert to DATE datatype
SELECT CONVERT(month,DATE)
FROM tablename; -- if I just run (1) and (2) i receive NULL
--- (3) Format back to yyyy-mm format
SELECT FORMAT(month,'yyyy-mm') AS month2
FROM tablename; -- If i run from (1) to (3), i receive 2,021
So:
How do i solve this problem conversion problem in MySQL
Is there anyway i could have prevented this before/while importing?
If you are looking for a one time migration from your string format to a SQL date or datetime, then you can just do
UPDATE tablename SET columname = CONCAT(columname, '-01');
and then update the column to a DATE or a DATETIME.
Alternatively if you do not want to update the values you could use the following queries to cast to a DATE or a DATETIME:
SELECT CAST(CONCAT(columnname, '-01') AS DATE) FROM tablename
or
SELECT CAST(CONCAT(columnname, '-01') AS DATETIME) FROM tablename
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.
I have a database column called time
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
But when I run the following query using PDO:
DELETE FROM `table` WHERE time < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL :days DAY))
It throws the following exception:
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '1555980012' for column 'time' at row 1
I don't understand why this is happening, could any one please explain?
You do not need to use UNIX_TIMESTAMP function in your where clause to convert date to number.
This should work with no issue:
DELETE FROM `table` WHERE `time` < DATE_SUB(NOW(), INTERVAL :days DAY)
Trying to understand how does ADDTIME() actually works.
Sample Table:
CREATE TABLE table3 (
id INT(11) NOT NULL AUTO_INCREMENT,
update_time DATETIME NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=latin1;
Sample data:
+-----+-----------------------+
| id | update_time |
+-----+-----------------------+
| 1 | 2017-09-13 18:55:24 |
| 2 | 2017-08-10 18:53:16 |
+-----+-----------------------+
If i executes the following query, it gives me error:
UPDATE table3 SET update_time = ADDTIME(NOW(), '60 00:00:00') WHERE id=1;
Error Code: 1292
Truncated incorrect time value: '60 00:00:00'
Although following query works fine:
UPDATE table3 SET update_time = ADDTIME(NOW(), '5 00:00:00') WHERE id=1;
Tried to keep increasing the value of DAY and found it works fine till DAY=34 and breaks for DAY > 34;
Could not found a clear answer why it breaks after a limit. Can some please explain how it is working?
PS: I know that DATE_ADD() can be used instead, I am using MySql 5.7.12.
From https://dev.mysql.com/doc/refman/5.7/en/time.html:
TIME values may range from '-838:59:59' to '838:59:59'
838:59:59 = 34 days 22 hours 59 minutes 59 seconds
According to the manual:
MySQL recognizes TIME values in these formats:
As a string in 'D HH:MM:SS' format. You can also use one of the following “relaxed” syntaxes: 'HH:MM:SS', 'HH:MM', 'D HH:MM', 'D HH',
or 'SS'. Here D represents days and can have a value from 0 to 34.
As a string with no delimiters in 'HHMMSS' format, provided that it makes sense as a time. For example, '101112' is understood as
'10:11:12', but '109712' is illegal (it has a nonsensical minute part)
and becomes '00:00:00'.
As a number in HHMMSS format, provided that it makes sense as a time. For example, 101112 is understood as '10:11:12'. The following
alternative formats are also understood: SS, MMSS, or HHMMSS.
The restriction is related to the range of the time type (and most likely the underlying storage requirements) as indicated in the answer by #ReneS
Reference
You cannot use DATE_ADD() instead of ADDTIME(), as the former would convert your TIMESTAMP/DATETIME values to DATE and loose the time parts...
Use TIMESTAMPADD() instead:
UPDATE table3 SET update_time = TIMESTAMPADD( DAY, 60, NOW() ) WHERE id = 1;
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).