简体   繁体   English

在DB2中发现时差

[英]finding time difference in DB2

How can I write a query in DB2 for following thing: 如何在DB2中为以下内容编写查询:

The difference between current timestamp and a timestamp field in dB should be >=4 hours AND <= 24 hours 当前时间戳和以dB为单位的时间戳字段之间的差应> = 4小时且<= 24小时

Someone suggested this but it's not working. 有人建议这样做,但没有用。

select * from tableName where 
                 date <=  DATEADD([hour], -4, CURRENT_TIME) and 
                 date date >=  DATEADD([hour], -24, CURRENT_TIME)

But it's not working. 但这不起作用。 It's giving following error. 它给出了以下错误。

SQL0104N  An unexpected token "[hour]" was found following "ortdate <=  
DATEADD(".  Expected tokens may include:  "<func_arg_list>".  SQLSTATE=42601
select * 
from   table t
where  t.tscolumn between current timestamp - 24 hours 
                      and current timestamp - 4 hours

Use just Hour instead of [hour] 仅使用Hour代替[hour]

select * from tableName where 
                 date <=  DATEADD(Hour, -4, CURRENT_TIME) and 
                 date date >=  DATEADD(Hour, -24, CURRENT_TIME)

DB2 doesn't like square brackets around name - that is a MS SQL Server mannerism. DB2不喜欢名称周围的方括号-这是MS SQL Server的习惯。

The only reference to DATEADD() in the DB2 9.7 Info Centre (oh, beg its pardon: Center - one day, American's will learn to spell correctly) is in 'All of the following expressions are in the package com.ibm.alphablox.bloxbuilder.lib.expression', which is puzzling. DB2 9.7信息中心中唯一对DATEADD()的引用(请原谅:中心-有一天,美国人会学会正确拼写)在“以下所有表达式都在com.ibm.alphablox包中。 bloxbuilder.lib.expression”,令人费解。 I suspect the search is erroneous - though going to the SQL Manual and finding the functions listed there, DATEADD is conspicuously absent, so maybe it isn't. 我怀疑搜索是错误的-尽管转到SQL手册并找到那里列出的功能,但显然没有DATEADD,所以也许不是。

So, you are going to have to manual bash for the DB2 syntax. 因此,您将不得不手动bash来使用DB2语法。 But, if anything is going to work, it is likely to involve: 但是,如果有任何事情起作用,则可能涉及:

DATEADD(HOUR, -4, CURRENT_TIME)

rather than any square brackets. 而不是任何方括号。 However, a somewhat more extensive search, including the RedBook on DB2 and Oracle Compatibility, does not show DATEADD as a function that is supported by DB2. 但是,在某种程度上更广泛的搜索,包括DB2上的RedBook和Oracle兼容性,并未将DATEADD显示为DB2支持的功能。 So, the DATEADD route is doomed to ... have problems. 因此,DATEADD路线注定会出现问题。

Since DB2 (still) doesn't have a proper (SQL standard) INTERVAL type, you are into investigating 'durations'. 由于DB2(仍然)没有适当的(SQL标准)INTERVAL类型,因此您正在研究“持续时间”。 See DevX for an explanation - but beware the number of cookies the site '.qnsr.com' wants to set. 有关说明,请参见DevX-但要注意网站“ .qnsr.com”要设置的Cookie数量。 And read the manuals at the DB2 Info Centre. 并在DB2信息中心阅读手册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM