在存储过程中将HH:MM:SS:Msec转换为HH:MM:SS
问题描述:
我有一个存储过程,该存储过程会基于这种计算来更新表,并且该计算以列名(日历)-(当前系统日期时间)完成,并将此信息更新为列(TimeSpent)并以Hh显示值:Mm:SS:Msec格式.查询工作正常,但我想以某种方式进行更新,以使花费的时间应仅为HH:MM:SS格式.请帮助我,我如何从所花费的时间中删除该Msec.
I have a stored procedure which update a table based on such calculation and the calculation is done as Column Name (Calendatedate) -( Current System Date Time) and update this information to a column (TimeSpent) and display the value in Hh:Mm:SS:Msec Format. The Query is working fine but i want to update it in such a way so that the Time spent should be only HH:MM:SS Format.Please help me that how i remove that Msec From the time Spent.
CREATE procedure St_Proc_UpdateTimeSpent
@timeEntryID int,
@status int output
as begin
set nocount on;
declare @Date dateTime;
set @Date=GETDATE();
update Production set TimeSpent=(SELECT CONVERT(VARCHAR(20),DateAdd(SS,Datediff(ss,CalendarDate, @Date)%(60*60*24),0),114)),
IsTaskCompleted=1
where productionTimeEntryID=@timeEntryID
set @status=1;
return @status;
end
答
在CONVERT函数中尝试使用108而不是114来仅获取hh :mm:ss格式.
参考: MSDN:CAST和CONVERT(Transact-SQL) [
Try 108 instead of 114 in the CONVERT function to get only the hh:mm:ss format.
Refer: MSDN: CAST and CONVERT (Transact-SQL)[^]