使用解码函数比较 Oracle 中的日期

发布于 2024-09-17 05:50:28 字数 463 浏览 22 评论 0原文

我需要使用 Oracle 解码函数比较两个日期,看看其中一个日期是否小于或等于另一个日期。

我发现这篇文章 - http://www.techonthenet.com/oracle/functions/decode .php

其中指出(在底部)如果 date1 > 则下面的解码函数将返回 date2 date2 :

decode((date1 - date2) - abs(date1 - date2), 0, date2, date1)

如果 date1 >= date2 ,这不会返回 date2 吗?

或者只是如果 date1 >日期2?

有更简单的解决方案吗?

I need to compare two dates using the Oracle decode function to see if one is less than or equal to the other.

I found this article - http://www.techonthenet.com/oracle/functions/decode.php

Which states (at the bottom) that the below decode function will return date2 if date1 > date2 :

decode((date1 - date2) - abs(date1 - date2), 0, date2, date1)

Would this not return date2 if date1 >= date2 ?

Or is it just if date1 > date2?

Is there an easier solution?

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

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

发布评论

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

评论(6

枯寂 2024-09-24 05:50:28

如果 date2 <= date1,该函数将返回 date2。代入这些值并转换为伪代码,您将得到 if 0 - 0 = 0 then date2 else date1,其中两个日期相同。


如果您使用 8i 或更高版本,更好的解决方案是使用 case

SELECT CASE WHEN date1 >= date2 THEN date2 ELSE date1 END FROM Your_Table;

由于 case 允许使用不等运算符,因此它的可读性要高得多。


或者,如果您想更简洁,可以使用旨在返回 n 值中较小者的函数:(

SELECT LEAST(date1, date2) FROM Your_Table;

还有一个 GREATEST 函数,其作用相反。)

That function will return date2 if date2 <= date1. Plugging in the values and translating to pseudo-code, you get if 0 - 0 = 0 then date2 else date1 where both dates are the same.


A better solution, if you're using 8i or later is to use case:

SELECT CASE WHEN date1 >= date2 THEN date2 ELSE date1 END FROM Your_Table;

Since case allows inequality operators, it's much more readable.


Or, if you want to be more succinct, you could use the function that's designed to return the lower of n values:

SELECT LEAST(date1, date2) FROM Your_Table;

(There is also a GREATEST function, which does the opposite.)

稀香 2024-09-24 05:50:28

@Allan 已经给了你最好的解决方案,但如果你坚持使用 decode 函数,你可以处理 sign 函数的结果。

http://www.techonthenet.com/oracle/functions/sign.php

如果 a sign(a) 返回 -1,则 a 如果a = 0,则为00;如果a > 则为1。 0 。 以下逻辑:

if date1 >= date2 then
    return date1;
else
    return date2;
end if;

因此,可以使用 decode 按以下方式重写

select decode(sign(date2-date1), 
              -1 /*this means date 1 > date 2*/, date1 /* return date1*/, 
               0 /*dates are equal */,           date1 /* again, return date1*/,
               /*in any other case, which is date2 > date1, return date2*/ date2) 
from dual;

@Allan has already given you the best solution to me, but if you insist on using decode function, you can process the result of sign function instead.

http://www.techonthenet.com/oracle/functions/sign.php

sign(a) returns -1 if a < 0, 0 if a = 0 and 1 if a > 0. Thus, the following logic

if date1 >= date2 then
    return date1;
else
    return date2;
end if;

could be rewritten using decode in the following way:

select decode(sign(date2-date1), 
              -1 /*this means date 1 > date 2*/, date1 /* return date1*/, 
               0 /*dates are equal */,           date1 /* again, return date1*/,
               /*in any other case, which is date2 > date1, return date2*/ date2) 
from dual;
温馨耳语 2024-09-24 05:50:28

您可以尝试 months_ Between 函数。它将计算两个日期之间的月数(十进制数)。

select months_between(sysdate+30, sysdate ) from dual;
select months_between(sysdate+15, sysdate ) from dual;

在此示例中,第一个参数大于第二个参数,因此它将返回 1。第二行返回 ~0.48(在 2010 年 9 月 1 日上午 11:30 左右执行时) 要获取实际日期值:

select case when months_between(sysdate+30, sysdate ) > 0 then sysdate+30 else sysdate end from dual;

一般情况下:

case when months_between(dateA, dateB ) > 0 then dateA else dateB

更新:

经过一些实验,该函数的最细粒度似乎是“Day”。

select months_between(to_date('2010-10-16 23:59:59', 'YYYY-MM-DD HH24:MI:SS'),
                       to_date('2010-10-16 00:00:00', 'YYYY-MM-DD HH24:MI:SS'))
from dual;

...将返回 0

select months_between(to_date('2010-10-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
                       to_date('2010-10-16 00:00:00', 'YYYY-MM-DD HH24:MI:SS'))
from dual;

将返回 0.032258064516129。

这里还有一些其他有趣的日期差异/比较技术: http://www.orafaq.com/faq/how_does_one_get_the_time_difference_ Between_two_date_columns< /a>

You could try the months_between function. It will calculate the number of months between two dates, as a decimal number.

select months_between(sysdate+30, sysdate ) from dual;
select months_between(sysdate+15, sysdate ) from dual;

In this example, the first paramater is greater than the second so it will return 1. The second line returns ~0.48 (when executed at about 11:30 AM on 2010-09-01) To get the actual date values:

select case when months_between(sysdate+30, sysdate ) > 0 then sysdate+30 else sysdate end from dual;

In general:

case when months_between(dateA, dateB ) > 0 then dateA else dateB

Update:

After some experimentation, it seems the finest granularity of this function is Day.

select months_between(to_date('2010-10-16 23:59:59', 'YYYY-MM-DD HH24:MI:SS'),
                       to_date('2010-10-16 00:00:00', 'YYYY-MM-DD HH24:MI:SS'))
from dual;

...will return 0

but

select months_between(to_date('2010-10-17 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
                       to_date('2010-10-16 00:00:00', 'YYYY-MM-DD HH24:MI:SS'))
from dual;

will return 0.032258064516129.

Some other interesting date difference/compare techniques here: http://www.orafaq.com/faq/how_does_one_get_the_time_difference_between_two_date_columns

孤独患者 2024-09-24 05:50:28

如果您尝试按日期检查 - 也就是说,1/1 中的每个时间都小于 1/2,并且 1/1 上的每个时间都等于 1/1 上的每个其他时间,即使 Oracle DATE 更大- 那么你想进行如下比较:

TRUNC(DATE1) <= TRUNC(DATE2)

我在其他答案中没有看到这一点,它是如此基本,让我想知道我是否误解了这个问题。

If you're trying to check by date - that is, every time in 1/1 is less than 1/2, and every on 1/1 is equal to every other time on 1/1, even if the Oracle DATE is greater - then you want to compare as follows:

TRUNC(DATE1) <= TRUNC(DATE2)

I don't see this in the other answers, it is so basic it makes me wonder if I'm misunderstanding the question.

豆芽 2024-09-24 05:50:28

这是更好的:

decode(sign(trunc(sysdate) - (trunc(sysdate))), 1, 1, -1, -1, 0 , 0)

1: date 1 > date 2
0: date 1 = date 2
-1: date 1 < date 2

This is better:

decode(sign(trunc(sysdate) - (trunc(sysdate))), 1, 1, -1, -1, 0 , 0)

1: date 1 > date 2
0: date 1 = date 2
-1: date 1 < date 2
一个人的夜不怕黑 2024-09-24 05:50:28

当 date1 >= date2 时返回 date2

will return date2 when date1 >= date2

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