从时间sql中减去特定时间段 [英] subtract specific time period from a time sql

查看:126
本文介绍了从时间sql中减去特定时间段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个时间(例如12:00:00)减去特定时间(例如1小时15分钟)。



目前我有试过



(TIMESTART - '01:30:00')

(TIMESTART - 013000)



任何帮助将不胜感激。在此先感谢

Im trying to subtract a specific time (eg. 1 hour 15 mins) from a time (eg. 12:00:00).

Currently i have tried

(TIMESTART - '01:30:00')
(TIMESTART - 013000)

Any help would be greatly appreciated. Thanks in advance

推荐答案

使用DATEADD()和DATEPART(),阅读: dateadd() [ ^ ]和

datepart() [ ^ ]

让我举一个例子,比如说一小时前:

Use DATEADD() and DATEPART(), read this: dateadd()[^] and
datepart()[^]
Let me give you one example, say one hour ago:
SELECT DATEPART(hour, DATEADD(hour, -1, GETDATE()))



尝试自己解决问题。


Try to work out the solution yourself.


你说这不行:



You said this does not work:

CASE
WHEN example = 'A' THEN DATEADD(hour, -1, TIMESTART)
    ELSE TIMESTART
    END AS STARTTIME





好​​吧,dateadd使用日期,并返回日期。 TIMESTART似乎是一个表示时间的字符串。



尝试





Well, dateadd works with a date, and returns a date. TIMESTART appears to be a string which represents a time.

try

CASE
WHEN example = 'A' THEN convert(time, DATEADD(hour, -1, TIMESTART))
    ELSE convert(time, TIMESTART)
    END AS STARTTIME





案例要求您返回的所有内容都具有相同的类型。



case requires everything you return has the same type.


这是最终有效的解决方案。谢谢你提出的所有建议。在DB2中,您实际上可以像这样添加/减去时间。



CASE

WHEN示例='A'那么TIMESTART - 30分钟

ELSE TIMESTART

END as STARTTIME
This is the solution that worked in the end. Thanks for all the suggestions though. In DB2 you can actually add/subtract time like this.

CASE
WHEN example = 'A' THEN TIMESTART - 30 MINUTES
ELSE TIMESTART
END AS STARTTIME


这篇关于从时间sql中减去特定时间段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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