SQL 日期时间差异(以天为单位)

发布于 2024-09-14 13:24:57 字数 368 浏览 1 评论 0原文

我想找出两个日期之间有多少天。我一直在尝试使用这个:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烈酒灼喉 2024-09-21 13:24:57

DATEDIFF 返回整数

但是,天数可能完全错误,具体取决于 LS_CHG_DTE_EQP/Report_Datelocale 的值/数据类型

此外,DATEDIFF 适用于日边界而不是 24 小时周期。因此,23:5600:02 是一天。

否则,请添加示例输入和输出。

DATEDIFF returns integer

However, the number of days could be plain wrong depending on the values/datatype of LS_CHG_DTE_EQP/Report_Date and locale

Also, DATEDIFF works on day boundaries not 24 hour periods. So 23:56 to 00:02 is one day.

Otherwise, please add example input and output.

梦开始←不甜 2024-09-21 13:24:57

示例

select DATEDIFF(dd,'20100101',getdate())

今天将返回 227,

因此在您的情况下假设 LS_CHG_DTE_EQP 和 Report_Date 都是日期时间列

SET @Days_Outstanding = DATEDIFF(day, LS_CHG_DTE_EQP, Report_Date)

Example

select DATEDIFF(dd,'20100101',getdate())

will return 227 today

so in your case assuming LS_CHG_DTE_EQP and Report_Date are both datetime columns

SET @Days_Outstanding = DATEDIFF(day, LS_CHG_DTE_EQP, Report_Date)
十雾 2024-09-21 13:24:57

你有没有尝试过

SET Days_Outstanding = CONVERT(datetime, LS_CHG_DTE_EQP, 1) - CONVERT(datetime, Report_Date, 1))

Have you tried just

SET Days_Outstanding = CONVERT(datetime, LS_CHG_DTE_EQP, 1) - CONVERT(datetime, Report_Date, 1))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文