PL/SQL 中的日期范围

发布于 2024-09-18 16:40:22 字数 264 浏览 4 评论 0原文

如果我有一个带有名为 created_date 的日期列(日期字段)的表,其值类似于“9/2/2010 5:25:42 PM”。

我想选择从 start_dateend_date 的所有行。但是,end_date 可能为null。在本例中,我想选择 created_date 大于 end_date 的所有行。

If I have table with a Date column (Date field) called created_date, with values like "9/2/2010 5:25:42 PM".

I want to select all rows from a start_date to a end_date. However, the end_date may be null. In this case, I want to select all rows where created_date is greater than end_date.

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

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

发布评论

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

评论(6

━╋う一瞬間旳綻放 2024-09-25 16:40:22

由于 toDate (可以为空)是一个主变量,它比已经给出的解决方案更容易(在这方面都是错误的,顺便说一句)

select * from mytable
  where created_date between v_fromdate
                         and nvl(v_todate, to_date('31.12.9999','dd.mm.yyyy'));

Since toDate (which can be null) is a host variable, it's easier than the solutions already given (which are all wrong in that regard, btw)

select * from mytable
  where created_date between v_fromdate
                         and nvl(v_todate, to_date('31.12.9999','dd.mm.yyyy'));
瑶笙 2024-09-25 16:40:22

选择 *
从表
其中created_date >= '2010-09-02'
并且(created_date 为NULL 或created_date <= '2010-09-03')

select *
from TABLE

where created_date >= '2010-09-02'
and (created_date is NULL or created_date <= '2010-09-03')

往事风中埋 2024-09-25 16:40:22

为什么只使用一个简单的 SQL 查询,就像这样:

select xxx from table_names where created_date is null or (created_date >= to_date("02/09/2010", "dd/mm/yyyy") and created_date <= to_date("03/09/2010", "dd/mm/yyyy"));

编辑

您可以定义一个类似于 ammoQ 定义的查询,即类似这样的查询:

select xxx from table_names where created_date is null or created_date >= start_date and created_date <= nvl(end_date, to_date("31/12/9999", "dd/mm/yyyy"));

但是,当您使用 PL/SQL,您可以检查 end_date 参数的可为空性:

IF end_date IS NULL THEN
    select xxx from table_names where created_date is null or created_date >= start_date;
ELSIF
    select xxx from table_names where created_date is null or created_date >= start_date and created_date <= end_date;
END IF;

请注意,如果 created_date 不为空,您可以删除 created_date is null 条件可为空的列...

Why just use a simple SQL query for that, like this one:

select xxx from table_names where created_date is null or (created_date >= to_date("02/09/2010", "dd/mm/yyyy") and created_date <= to_date("03/09/2010", "dd/mm/yyyy"));

Edit

You can define a query like the one defined by ammoQ, i.e. something like that:

select xxx from table_names where created_date is null or created_date >= start_date and created_date <= nvl(end_date, to_date("31/12/9999", "dd/mm/yyyy"));

However, as you are using PL/SQL, you can check the nullability of end_date parameter:

IF end_date IS NULL THEN
    select xxx from table_names where created_date is null or created_date >= start_date;
ELSIF
    select xxx from table_names where created_date is null or created_date >= start_date and created_date <= end_date;
END IF;

Note that you can remove the created_date is null condition if created_date is not a nullable column...

枯寂 2024-09-25 16:40:22
select * from yourtable
where created_date >= @StartDate AND created_date <=ISNULL(@EndDate,created_date)
select * from yourtable
where created_date >= @StartDate AND created_date <=ISNULL(@EndDate,created_date)
ˉ厌 2024-09-25 16:40:22
SELECT *
  FROM A_TABLE
  WHERE CREATED_DATE >= &START_DATE AND
        (CREATED_DATE <= &END_DATE OR
         &END_DATE IS NULL)
SELECT *
  FROM A_TABLE
  WHERE CREATED_DATE >= &START_DATE AND
        (CREATED_DATE <= &END_DATE OR
         &END_DATE IS NULL)
凡间太子 2024-09-25 16:40:22

如果我从你的问题中得到它
这应该有效:

SELECT *
FROM yourTable
WHERE created_date >= to_date('01.09.2010', 'dd.mm.yyyy')
  AND (end_date  <= to_date('02.09.2010', 'dd.mm.yyyy')
  OR end_date   IS NULL);

If i took it right from your question
this should work:

SELECT *
FROM yourTable
WHERE created_date >= to_date('01.09.2010', 'dd.mm.yyyy')
  AND (end_date  <= to_date('02.09.2010', 'dd.mm.yyyy')
  OR end_date   IS NULL);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文