将字符串转换为秒

时间:2019-03-26 10:14:05

标签: sql sql-server

我想以秒为单位的时间持续一天更新超过24小时,应该在新列中以秒为单位更新

update [dbo].[DurationLoad]
set [DurationInSeconds] = (DATEDIFF(SECOND, '0:00:00', [Duration_LoadValue] )) 
where [Duration_LoadValue] is not null

Msg 241,第16级,状态1,第26行 从字符串转换日期和/或时间时转换失败。

CREATE TABLE [dbo].[DurationLoad](
[Sno] [int] IDENTITY(1,1) NOT NULL,
[Duration_LoadValue] [nvarchar](7) NULL,
[DurationInSeconds] [decimal](10, 0) NULL
) ON [PRIMARY]
GO 

 INSERT [dbo].[DurationLoad] ([Sno], [Duration_LoadValue], [DurationInSeconds]) 
VALUES 
(1, N' 21:50', NULL),
(2, N' 23:38', NULL),
(3, N' 27:50', NULL),
(4, N' 33:47', NULL),
(5, N'-114.25', NULL),
(6, N'75:15', NULL),
(7, N'-126.34', NULL) 




Select  [Duration_LoadValue]  , (DATEDIFF(SECOND, '0:00:00', [Duration_LoadValue] )) 
FROM [dbo].[DurationLoad]
where [Duration_LoadValue] is not null

update [dbo].[DurationLoad]
set [DurationInSeconds] = (DATEDIFF(SECOND, '0:00:00', [Duration_LoadValue] )) 
where [Duration_LoadValue] is not null

Msg 241,第16级,状态1,第26行 从字符串转换日期和/或时间时转换失败。

select * from [dbo].[DurationLoad]

上述值的持续时间以秒为单位

enter image description here

0 个答案:

没有答案