DATEADD 使用小数 - 解决方法?

发布于 2024-12-18 08:18:33 字数 404 浏览 2 评论 0原文

我正在尝试做类似下面的事情(我简化了问题,尝试解决这个单独的部分)。

PRINT DATEADD(week, 0.2,  GETDATE())

我意识到这是行不通的,因为 dateadd 的数字参数被截断为 int。

我试图想出一种将 0.2 转换为小时数的方法,然后能够使用类似的方法(我同意它精确到小时)。

PRINT DATEADD(hour, X,  GETDATE())

有什么想法如何开始吗?我发现很难找到任何东西来获取另一个日期部分中一个日期部分的十进制表示形式。

它必须是一个基于集合的性能查询。

理论上我必须做到这一点,以便也可以使用十进制年份,但我将把它作为一个单独的问题来解决......

I'm trying to do something like below (I've simplified the problem, to try and solve this individual part).

PRINT DATEADD(week, 0.2,  GETDATE())

Which I realise will not work due to the number parameter of dateadd is truncated to an int.

I'm trying to come up with a way of converting 0.2 to a number of hours and then being able to use something like (I'm ok with it being as accurate to the hour).

PRINT DATEADD(hour, X,  GETDATE())

Any ideas how to get started? I'm finding it tricky to find anything to get a decimal representation of one datepart in another datepart.

It's got to be a set based query for performance.

In theory I've got to make it so decimal years can be used as well, but I'll come to that as a seperate problem...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小红帽 2024-12-25 08:18:33

1 周 = 7 天 = 168 小时 = 10080 分钟

0.2 周 = 2016 分钟

PRINT DATEADD(min, 2016, GETDATE())

要让它工作多年,您可以编写一个 User定义函数,您可以在其中输入年的小数,然后将其乘以分钟,然后使用运行DATEADD 分钟

1 week = 7 days = 168 hours = 10080 minutes

0.2 weeks = 2016 minutes

PRINT DATEADD(minute, 2016, GETDATE())

To get this to work for years, you could write a User Defined Function where you can input a decimal of years, and then this is multiplied for minutes, and run DATEADD using minutes

薄荷梦 2024-12-25 08:18:33

这就是我想出的,以防它对任何人都有用。柯特帮助我睁开了眼睛,让我认识到我的问题实际上并不像我最初想象的那么复杂。我已将 Curt 的答案标记为解决方案,但这里有一些示例代码。值得注意的是,10% 不在我最初的问题中,但它说明了为什么我需要小数。

DECLARE @DatePart int,
        @DateNumber int;

SET @DatePart = 1
SET @DateNumber = 1                         

PRINT
    DATEADD(
                hour,
                CEILING
                (           
                    CASE @DatePart
                        WHEN 1 THEN @DateNumber * 24.000000 --days
                        WHEN 2 THEN @DateNumber * 168.000000 --weeks
                        WHEN 3 THEN @DateNumber * 730.484398 --months
                        WHEN 4 THEN @DateNumber * 8765.81277 --years
                    END / 100.000000 * 10.000000
                ),
                GETDATE()
            ) 

This is what I came up with incase it comes in useful for anyone. Curt helped me open my eyes to that my problem was infact not as complex as I first thought. I've marked Curt's answer as the solution, but here is some sample code. Notably the 10% wasn't in my original question but it demonstrates why I needed decimals.

DECLARE @DatePart int,
        @DateNumber int;

SET @DatePart = 1
SET @DateNumber = 1                         

PRINT
    DATEADD(
                hour,
                CEILING
                (           
                    CASE @DatePart
                        WHEN 1 THEN @DateNumber * 24.000000 --days
                        WHEN 2 THEN @DateNumber * 168.000000 --weeks
                        WHEN 3 THEN @DateNumber * 730.484398 --months
                        WHEN 4 THEN @DateNumber * 8765.81277 --years
                    END / 100.000000 * 10.000000
                ),
                GETDATE()
            ) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文