[英]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.