从 SQL Server 2005 中的日期时间减去分钟

发布于 2024-08-22 01:14:02 字数 560 浏览 4 评论 0原文

假设我有一个日期时间字段,其值为 2000-01-01 08:30:00 和一个值为 00:15 (意思是 15 分钟)的持续时间字段,

如果我减去这两个,我应该得到 2000-01-01 08:15:00

另外如果我想减去1:15(意味着1小时15分钟),输出应该是2000-01-01 07:15:00

我正在尝试SELECT DATEDIFF(min, '00:15','2000-01-01 08:30:00');

但输出是 52595055。我怎样才能得到想要的结果?

注意:如果我执行 SELECT dateadd(month, -15,'2000-01-01 08:30:00'); ,我将得到所需的结果,但这涉及解析分钟字段。

编辑:

根据答案,每个人都建议将所有内容转换为分钟,然后减去 - 所以如果是 1:30,我需要减去 90 分钟。没关系。有没有其他方法而不转换为分钟?

Suppose I have a datetime field whose value is 2000-01-01 08:30:00
and a duration field whose value is say 00:15 (meaning 15 minutes)

If I subtract these two, I should get 2000-01-01 08:15:00

Also if I want to subtract 1:15 (means 1 hour 15 minutes), the output should be 2000-01-01 07:15:00

I am trying SELECT DATEDIFF(minute, '00:15','2000-01-01 08:30:00');

But the output is 52595055. How can i get the desired result?

N.B.~ If I do SELECT dateadd(minute, -15,'2000-01-01 08:30:00'); , I will get the desired result but that involves parsing the minute field.

Edit:

As per the answers, every one is suggesting converting everything into minutes and then to subtract - so if it is 1:30, i need to subtract 90 minutes. That's fine. Any other way without converting to minutes?

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

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

发布评论

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

评论(5

美煞众生 2024-08-29 01:14:02
SELECT DATEADD(minute, -15, '2000-01-01 08:30:00'); 

第二个值(本例中为-15)必须是数字(即不是像“00:15”这样的字符串)。如果您需要减去小时和分钟,我建议在 : 上拆分字符串以获取小时和分钟,并使用类似的方法进行减去,

SELECT DATEADD(minute, -60 * @h - @m, '2000-01-01 08:30:00'); 

其中 @h 是字符串的小时部分,@m 是字符串的分钟部分

编辑:

这是一个更好的方法:

SELECT CAST('2000-01-01 08:30:00' as datetime) - CAST('00:15' AS datetime)
SELECT DATEADD(minute, -15, '2000-01-01 08:30:00'); 

The second value (-15 in this case) must be numeric (i.e. not a string like '00:15'). If you need to subtract hours and minutes I would recommend splitting the string on the : to get the hours and minutes and subtracting using something like

SELECT DATEADD(minute, -60 * @h - @m, '2000-01-01 08:30:00'); 

where @h is the hour part of your string and @m is the minute part of your string

EDIT:

Here is a better way:

SELECT CAST('2000-01-01 08:30:00' as datetime) - CAST('00:15' AS datetime)
櫻之舞 2024-08-29 01:14:02

您想要使用 DATEADD,并使用负持续时间。例如

DATEADD(minute, -15, '2000-01-01 08:30:00') 

You want to use DATEADD, using a negative duration. e.g.

DATEADD(minute, -15, '2000-01-01 08:30:00') 
小情绪 2024-08-29 01:14:02

您是否尝试过

SELECT DATEADD(MINUTE, -15,'2000-01-01 08:30:00')

DATEDIFF 是两个日期之间的差异。

Have you tried

SELECT DATEADD(MINUTE, -15,'2000-01-01 08:30:00')

DATEDIFF is the difference between 2 dates.

白日梦 2024-08-29 01:14:02

我花了一段时间尝试做同样的事情,试图从datetime中减去小时:分钟 - 这是我的做法:

convert( varchar, cast((RouteMileage / @average_speed) as integer))+ ':' +  convert( varchar, cast((((RouteMileage / @average_speed) - cast((RouteMileage / @average_speed) as integer)) * 60) as integer)) As TravelTime,

dateadd( n, -60 * CAST( (RouteMileage / @average_speed) AS DECIMAL(7,2)), @entry_date) As DepartureTime 

输出:

DeliveryDate                TravelTime             DepartureTime
2012-06-02 12:00:00.000       25:49         2012-06-01 10:11:00.000

I spent a while trying to do the same thing, trying to subtract the hours:minutes from datetime - here's how I did it:

convert( varchar, cast((RouteMileage / @average_speed) as integer))+ ':' +  convert( varchar, cast((((RouteMileage / @average_speed) - cast((RouteMileage / @average_speed) as integer)) * 60) as integer)) As TravelTime,

dateadd( n, -60 * CAST( (RouteMileage / @average_speed) AS DECIMAL(7,2)), @entry_date) As DepartureTime 

OUTPUT:

DeliveryDate                TravelTime             DepartureTime
2012-06-02 12:00:00.000       25:49         2012-06-01 10:11:00.000
诠释孤独 2024-08-29 01:14:02

使用 DATEPART 来拉开您的时间间隔,和 DATEADD 减去部分:

select dateadd(
     hh,
    -1 * datepart(hh, cast('1:15' as datetime)),
    dateadd(
        mi,
        -1 * datepart(mi, cast('1:15' as datetime)),
        '2000-01-01 08:30:00'))

或者,我们可以转换为分钟首先(尽管OP不想这样做):

declare @mins int
select @mins = datepart(mi, cast('1:15' as datetime)) + 60 * datepart(hh, cast('1:15' as datetime)) 
select dateadd(mi, -1 * @mins, '2000-01-01 08:30:00')

Use DATEPART to pull apart your interval, and DATEADD to subtract the parts:

select dateadd(
     hh,
    -1 * datepart(hh, cast('1:15' as datetime)),
    dateadd(
        mi,
        -1 * datepart(mi, cast('1:15' as datetime)),
        '2000-01-01 08:30:00'))

or, we can convert to minutes first (though OP would prefer not to):

declare @mins int
select @mins = datepart(mi, cast('1:15' as datetime)) + 60 * datepart(hh, cast('1:15' as datetime)) 
select dateadd(mi, -1 * @mins, '2000-01-01 08:30:00')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文