1、报错的sql为:
select
ID,
Test_time as 时间,
from ProcessData
where
convert(datetime,test_time,120) between convert(datetime, '2020-10-10', 120) and convert(datetime, '2024-10-11', 120)
它是将Test_time转化为datetime格式,再用between进行比较;
2、报错原因:
是由convert()函数报错引起,需要定位到哪条数据报错,修正那条数据。
3、对策:
(1)定位报错源
用于定位是表内哪条数据报错的sql如下:
select
T1.ID,
T1.ConversionStatus
from
(select
ID,
CASE WHEN TRY_CAST(Test_time AS DATE) IS NULL
THEN '无法转换' ELSE '可以转换'END AS ConversionStatus
from ProcessData
where test_time >'2024-10-08'
) T1
where ConversionStatus='无法转换'
(2)修正/删除
我这里不需要这个数据,直接删除即可。
delete from ProcessData
where test_time >'2024-10-08' AND ID='2'
标签:10,convert,转换,datetime,报错,time,字符串,ID,MSSQL From: https://www.cnblogs.com/qq2806933146xiaobai/p/18456025