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 as DATE.
—SA

>