如何解决如何从 SQL Server 中删除转换失败的日期/时间错误?
Msg 241,Level 16,State 1,Procedure SumByDay,Line 7 [Batch Start Line 36]
从字符串转换日期和/或时间时转换失败。
这是流程
ALTER PROCEDURE SumByDay (@IN_forDate AS DATE,@IN_sensID INT)
AS
BEGIN
DECLARE @timeFrom DATETIME2;
DECLARE @timeto DATETIME2;
DECLARE @rowCount INT = 0; -- sqlInes DEMO *** statement affected rows
SET @timeFrom = concat(CAST(@IN_forDate AS DATETIME2),CAST('00:00:00' AS TIME)); -- start of the day
SET @timeto = concat(CAST(@IN_forDate AS DATETIME2),CAST('23:59:59.999999' AS TIME)); -- end of the day
DELETE FROM TrafficPerDay
WHERE TrafficPerDay.sensorID = @IN_sensID
AND TrafficPerDay.sDay = @IN_forDate; -- sqlInes DEMO *** sts,delete it,and recreate with new data
INSERT INTO TrafficPerDay(sensorID,sDay,Motorcycle,Car,Van,Bus,Coach,Truck,ArticulatedTruck,Semitruck,Pedestrian,[UnkNown],sTotal)
(SELECT
t.sensorID,@IN_forDate AS 'sDay',COUNT(CASE WHEN t.classId = 1 THEN 'id' END) 'Motorcycle',COUNT(CASE WHEN t.classId = 2 THEN 'id' END) 'Car',COUNT(CASE WHEN t.classId = 3 THEN 'id' END) 'Van',COUNT(CASE WHEN t.classId = 4 THEN 'id' END) 'Bus',COUNT(CASE WHEN t.classId = 5 THEN 'id' END) 'Coach',COUNT(CASE WHEN t.classId = 6 THEN 'id' END) 'Truck',COUNT(CASE WHEN t.classId = 7 THEN 'id' END) 'ArticulatedTruck',COUNT(CASE WHEN t.classId = 8 THEN 'id' END) 'Semitruck',COUNT(CASE WHEN t.classId = 9 THEN 'id' END) 'Pedestrian',COUNT(CASE WHEN t.classId = 0 THEN 'id' END) 'UnkNown',COUNT(t.id) AS 'sTotal'
FROM
TrafficRecord t
WHERE
t.time BETWEEN @timeFrom AND @timeto AND t.sensorId = @IN_sensID
GROUP BY t.sensorID,t.time );
-- sqlInes LICENSE FOR EVALUATION USE ONLY
SELECT @rowCount = @@ROWCOUNT
IF (@rowCount = 0)
BEGIN -- sqlInes DEMO *** ed,insert zeros
INSERT INTO TrafficPerDay(sensorID,sTotal)
VALUES (@IN_sensID,@IN_forDate,0);
END
END
我正在执行这样的程序
exec SumByDay @IN_forDate = '2020-11-04',@IN_sensID = 1;
解决方法
问题是你在第 7 行的连接,正如错误告诉你的那样,但你不需要那个。使用 >=
和 <
逻辑代替 BETWEEN
:
ALTER PROCEDURE SumByDay (@IN_forDate AS date,@IN_sensID int)
AS
BEGIN
--DECLARE @timeFrom datetime2; -- No need for these
--DECLARE @timeTo datetime2; -- No need for these
DECLARE @rowCount int = 0; -- SQLINES DEMO *** statement affected rows
--SET @timeFrom = CONCAT(CAST(@IN_forDate AS datetime2),CAST('00:00:00' AS time)); -- start of the day -- No need for these
--SET @timeTo = CONCAT(CAST(@IN_forDate AS datetime2),CAST('23:59:59.999999' AS time)); -- end of the day -- No need for these
DELETE FROM TrafficPerDay
WHERE TrafficPerDay.sensorID = @IN_sensID
AND TrafficPerDay.sDay = @IN_forDate; -- SQLINES DEMO ***
--sts,delete it,and recreate with new data
INSERT INTO TrafficPerDay (sensorID,sDay,Motorcycle,Car,Van,Bus,Coach,Truck,ArticulatedTruck,Semitruck,Pedestrian,[Unknown],sTotal)
(SELECT t.sensorID,@IN_forDate AS sDay,COUNT(CASE WHEN t.classId = 1 THEN 'id' END) AS Motorcycle,COUNT(CASE WHEN t.classId = 2 THEN 'id' END) AS Car,COUNT(CASE WHEN t.classId = 3 THEN 'id' END) AS Van,COUNT(CASE WHEN t.classId = 4 THEN 'id' END) AS Bus,COUNT(CASE WHEN t.classId = 5 THEN 'id' END) AS Coach,COUNT(CASE WHEN t.classId = 6 THEN 'id' END) AS Truck,COUNT(CASE WHEN t.classId = 7 THEN 'id' END) AS ArticulatedTruck,COUNT(CASE WHEN t.classId = 8 THEN 'id' END) AS Semitruck,COUNT(CASE WHEN t.classId = 9 THEN 'id' END) AS Pedestrian,COUNT(CASE WHEN t.classId = 0 THEN 'id' END) AS Unknown,COUNT(t.id) AS sTotal
FROM TrafficRecord t
WHERE t.time >= @IN_forDate
AND t.time < DATEADD(DAY,1,@IN_forDate)
AND t.sensorId = @IN_sensID
GROUP BY t.sensorID,t.time); --Should this be CONVERT(date,t.time),--or considering that this is for a single day,removed?
--If not,then simply t.time = @IN_forDate would work
-- SQLINES LICENSE FOR EVALUATION USE ONLY
SELECT @rowCount = @@ROWCOUNT;
IF (@rowCount = 0)
BEGIN -- SQLINES DEMO *** ed,insert zeros
INSERT INTO TrafficPerDay (sensorID,sTotal)
VALUES (@IN_sensID,@IN_forDate,0);
END;
END;
,
您设置的 datetime2
值不正确。
通过将时间连接到您已经转换为 datetime2
的日期,您试图然后将 2021-03-18 00:00:00.000000000:00:00.0000000
之类的日期分配给 @timeFrom
- 显然不正确。
只需做@timeFrom=Concat(@IN_forDate,' 00:00:00')
等
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。