Solution to sql execution error Truncated incorrect time value

I. Introduction

The production database was recently modified and a batch of data needs to be updated.

1. First, execute this sql:

select count(*) from class_subject 
where endtime is not null and starttime is not null and round(TIME_TO_SEC(TIMEDIFF(endtime,starttime))/60/60,1)!=round(hour,1)

It’s the data under query 结束时间-开始时间!=hour. There is no problem with this execution.

2. Then, when executing this sql, an error is reported:

update class_subject set hour=round(TIME_TO_SEC(TIMEDIFF(endtime,starttime))/60/60,1)
where endtime is not null and starttime is not null and round(TIME_TO_SEC(TIMEDIFF(endtime,starttime))/60/60,1)!=round(hour,1)

I just want to set it up hour=结束时间-开始时间, but I get an error:

SQL错误[1292][22001]:Data truncation:Truncated incorrect time value:
'2616:30:00'

2. Investigation process

Baidu found that some said:

1. The fundamental reason is that this function calculates the time difference between two dates, but its maximum value is only up to 838:59:59 (less than 35 days). If it exceeds, an error will be reported.

However, we checked the database data and round(TIME_TO_SEC(TIMEDIFF(endtime,starttime))/60/60,1)found that the difference was only 3 hours at most and did not exceed 35 days.

3. Solution

First of all, our goal is to update a batch of data in the database. Secondly, we found that there is no problem in executing select, so we decided to query the IDs and values ​​to be updated of all the data, and update them in batches one by one.

1. Using sql:

select id, round(TIME_TO_SEC(TIMEDIFF(endtime,starttime))/60/60,1)!=round(hour,1) as hour from class_subject 
where endtime is not null and starttime is not null and round(TIME_TO_SEC(TIMEDIFF(endtime,starttime))/60/60,1)!=round(hour,1)

In this way, you can query the ID of the data to be modified and the target hour value that needs to be modified.

2. Use excel to batch splice the data into the following format:

update class_subject set hour='xxx' where id='xxx';
update class_subject set hour='xxx' where id='xxx';
update class_subject set hour='xxx' where id='xxx';

3. Then, batch update one by one, bypassing this error and updating the data successfully.

Guess you like

Origin blog.csdn.net/BHSZZY/article/details/133387130