SQL 日期时间差异(以天为单位)
我想找出两个日期之间有多少天。我一直在尝试使用这个:
SET Days_Outstanding = DATEDIFF(day, CONVERT(datetime, LS_CHG_DTE_EQP, 1), CONVERT(datetime, Report_Date, 1))
但是,这并没有在几天内返回数字。它返回的日期时间与我的数据集无关。
我不确定问题出在哪里,对于 SQL 来说还是相当新的。另外,我将 Days_Outstanding 定义为 int,这可能是问题所在吗?谢谢!
编辑
谢谢大家!不知道发生了什么,我按照你的建议做了一些改变,当我回到原来的状态时,它就开始工作了。不知道发生了什么。谢谢想!
I am trying to find how many days are between two dates. I have been attempting to use this:
SET Days_Outstanding = DATEDIFF(day, CONVERT(datetime, LS_CHG_DTE_EQP, 1), CONVERT(datetime, Report_Date, 1))
However, this is not returning the number in days. It's returning a datetime in years that have nothing to do with my data set.
I'm not sure where the problem is, still fairly new to SQL. Also, I have Days_Outstanding defined as an int, could that be the problem? Thanks!
EDIT
Thanks everyone! Not sure what happened, I changed a few things to what you suggested and it wound up working when I went back to what I had originally. Not sure what happened. Thanks thought!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DATEDIFF
返回整数但是,天数可能完全错误,具体取决于
LS_CHG_DTE_EQP/Report_Date
和locale
的值/数据类型此外,
DATEDIFF
适用于日边界而不是 24 小时周期。因此,23:56
到00:02
是一天。否则,请添加示例输入和输出。
DATEDIFF
returns integerHowever, the number of days could be plain wrong depending on the values/datatype of
LS_CHG_DTE_EQP/Report_Date
andlocale
Also,
DATEDIFF
works on day boundaries not 24 hour periods. So23:56
to00:02
is one day.Otherwise, please add example input and output.
示例
今天将返回 227,
因此在您的情况下假设 LS_CHG_DTE_EQP 和 Report_Date 都是日期时间列
Example
will return 227 today
so in your case assuming LS_CHG_DTE_EQP and Report_Date are both datetime columns
你有没有尝试过
Have you tried just