调试经验——使用Oracle BETWEEN函数限定查询结果的时间范围(Limit data range in Oracle)

需求:

要创建一个半年生成一次的报表给领导看。现有的做法是在数据库中提取出数据后在Excel中通过filter筛选出最近半年的数据,没错,纯手动操作(弊端有二:容易出错,效率低)。如何自动化呢?使用VBA有点小题大做,通过SQL脚本实现即可。该BETWEEN条件语句出场了。

代码:

AND UTABLE.UDATEFIELD BETWEEN TO_DATE('2017-12-01','YYYY-MM-DD') BETWEEN TO_DATE('2018-05-31','YYYY-MM-DD')

一枪头搞定,哈哈。

顺便看看Oracle Database SQL Language Reference中对BETWEEN CONDITION的描述:

----------------------------------------------------------------------------------------------------------------

BETWEEN Condition

A BETWEEN condition determines whether the value of one expression is in an interval
defined by two other expressions.


All three expressions must be numeric, character, or datetime expressions. In SQL, it is
possible that expr1 will be evaluated more than once. If the BETWEEN expression
appears in PL/SQL, expr1 is guaranteed to be evaluated only once. If the expressions
are not all the same data type, then Oracle Database implicitly converts the
expressions to a common data type. If it cannot do so, then it returns an error.

The value of
expr1 NOT BETWEEN expr2 AND expr3
is the value of the expression
NOT (expr1 BETWEEN expr2 AND expr3)
And the value of
expr1 BETWEEN expr2 AND expr3
is the value of the boolean expression:
expr2 <= expr1 AND expr1 <= expr3
If expr3 < expr2, then the interval is empty. If expr1 is NULL, then the result is NULL. If
expr1 is not NULL, then the value is FALSE in the ordinary case and TRUE when the
keyword NOT is used.
The boolean operator AND may produce unexpected results. Specifically, in the
expression x AND y, the condition x IS NULL is not sufficient to determine the value of
the expression. The second operand still must be evaluated. The result is FALSE if the
second operand has the value FALSE and NULL otherwise. See "Logical Conditions" on
page 7-8 for more information on AND.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值