频道栏目
首页 > 资讯 > Oracle > 正文

使用Oracle BETWEEN函数限定查询结果的时间范围(LimitdatarangeinOracle)的调试经验分享

18-06-23        来源:[db:作者]  
收藏   我要投稿

需求:

要创建一个半年生成一次的报表给领导看。现有的做法是在数据库中提取出数据后在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.

相关TAG标签
上一篇:插入排序的具体实现教程
下一篇:mysql基础学习之外键(foreignkey)
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站