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
You can just use style 108 instead of 114 in the
CONVERTfunction to get only thehh:mm:ss:See the excellent MSDN documentation on CAST and CONVERT for a comprehensive list of all supported styles when converting
DATETIMEtoVARCHAR(and back)BTW: SQL Server 2008 also introduced a
TIMEdatatype which would probably be a better fit than aVARCHARto store yourTimeSpentvalues … check it out!