简体   繁体   English

以小时、分钟、秒为单位返回的两个日期之间的差异

[英]Difference between two dates returned in hours,minutes,seconds

The following query returns dates in terms of hours,minutes,seconds.以下查询以小时、分钟、秒的形式返回日期。

SELECT LEFT(CONVERT(VARCHAR(10),  ModifiedOn- CreatedOn, 108), 8)
AS ResultTime from AssignedRoles

I have an extra field in the table AssignedRoles -"TurnAroundTime" where i would like to store the result generated from the query.我在表 AssignedRoles -“TurnAroundTime”中有一个额外的字段,我想在其中存储从查询生成的结果。 Is this method effective, or is using a trigger a better option and if so kindly explain how to undertake it.这种方法是否有效,或者使用触发器是更好的选择,如果是,请解释如何进行。

The database is a backend to an MVC C# application该数据库是 MVC C# 应用程序的后端

I agree with @GMB (+1).我同意@GMB (+1)。 However, you are storing (or calculating) this value as a formatted character string.但是,您将此值存储(或计算)为格式化字符串。 I recommend storing it as a TIME datatype value, as that will give you more flexibility and options in the future.我建议将其存储为 TIME 数据类型值,因为这将在未来为您提供更多的灵活性和选择。 Let the application using this data worry about the formatting, it will do a better job of it.让使用这些数据的应用程序担心格式化,它会做得更好。

ALTER TABLE AssignedRoles add 
 turnAroundtime as CONVERT(TIME, ModifiedOn - CreatedOn)

I would recommend a computed column:我会推荐一个计算列:

alter table AssignedRoles add 
    turnAroundtime as (left(convert(varchar(10), ModifiedOn - CreatedOn, 108), 8))

This gives you an always up-to-date value that you don't need to maintain manually, with almost zero extra code.这为您提供了不需要手动维护的始终最新的值,几乎为零的额外代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找小时和分钟两个日期之间的时差 - Find the difference between two dates in hours and minutes oracle中同一列中两个日期之间的差异,以天/小时/分钟为单位 - The difference between two dates in the same column in oracle in days/Hours/minutes 计算两个日期之间的分钟时间差(仅限工作时间) - Calculate time difference (only working hours) in minutes between two dates select 两个日期的时差和分 - select difference of two dates in hours and minutes 如何在两个日期之间显示小时和分钟 - How to display Hours and Minutes between two dates 分钟和小时之间的两个日期之间的时差,例如SQL Server 2012中的1hr 20Min - Time Difference between two dates in Minutes and hours like 1hr 20Min in SQL Server 2012 如何在工作日 SQL Server 的工作时间内计算两个日期之间的差异(以秒为单位)? - How to calculate difference between two dates in seconds during business hours for business days SQL Server? 以分钟为单位获取两个日期之间的平均差 - Getting average difference between two dates in minutes 获得两个日期之间的小时数差异 - Get difference in hours between two dates 获取两个日期之间的天,小时和分钟,分别带有时间戳 - Getting Days, Hours and Minutes between two dates with their respective time stamps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM