如何计算sql querry中日期和时间的总小时数和天数? [英] how to count total number of hours and days from date and time in sql querry?

查看:208
本文介绍了如何计算sql querry中日期和时间的总小时数和天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

how to count total number of hours and days from date and time in sql querry?


My table Is-


Id CheckInDate   CheckInTime     CheckOutDate    CheckOutTime

1  2014-12-28   09 : 29 : AM    2014-12-30  06 : 29 : AM

2  2014-12-27   11 : 30 : AM    2014-12-30  12 : 30 : PM



Please solve this problem?

推荐答案

更改数据库。

停止存储日期和时间为NVARCHAR,并单独停止存储。始终将日期和时间存储在一起,因此它们在DATETIME变量中形成一个时刻。

然后您可以使用 DATEDIFF函数 [ ^ ]返回你想要的东西。



如果你把它们存储为字符串,你必须每次想要使用它们来验证和转换它们,那就是坏。设法进入数据库的单个错误会使您的所有查询陷入混乱。
Change your database.
Stop storing dates and times as NVARCHAR, and stop storing them separately. Always store dates and times together, so they form a single "moment in time" in DATETIME variables.
Then you can just use the DATEDIFF function[^] which returns what you want.

If you store them as strings, you have to validate and convert them every single time you want to use them, and that's bad. A single error that manages to get into your DB will mess up all your queries.


这篇关于如何计算sql querry中日期和时间的总小时数和天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆