Nvarchar时间计算 [英] Nvarchar time calculation
本文介绍了Nvarchar时间计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个nvarchar字段,时间数据为12:34:34,第二个是12:34我想计算小时数的差异。如果名称重要,则调用第一个字段(OTIM),调用第二个字段(ReportedTime)。有人请帮助我。我尝试使用substring来修剪OTIM,我无法使其正常工作。
I have two nvarchar fields with time data 12:34:34 and the second one 12:34 I want to calculate the difference in Hours. The first field is called (OTIM) the second field is called (ReportedTime) if the name matters. Some one please help me. I tried substring to trim the OTIM, I am unable to make it work.
推荐答案
-- With Specified time
SELECT DATEDIFF(mi, '12:34:34', '12:34')/60;
-- with Specified Column Names
SELECT DATEDIFF(mi, OTIM, ReportedTime)/60;
谢谢,
Baliram Suryawanshi
Thanks,
Baliram Suryawanshi
试试
try
SELECT DATEDIFF(HOUR, CAST(OTIM AS DATETIME),CAST(ReportedTime AS DATETIME)) FROM YourTableName
如果你是使用SQL Server 2005/2008然后这将工作,我没有在其他版本中测试
If you are using SQL server 2005/2008 then this will work, I did not test in other versions
从不将时间数据存储在字符串属性中。使用适当的日期/时间SQL数据类型,就像DATE
。
-SA
Never store time data in string attributes. Use appropriate date/time SQL data types, just asDATE
.
—SA
>
这篇关于Nvarchar时间计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文