在过去4年的一月到三月之间获取数据

发布于 2025-02-14 01:47:56 字数 123 浏览 0 评论 0原文

我最近四年的数据是2018,2019,2020,2021。 在此过程中,我需要一月至3月的数据,2018年,2019,2020,2021。

我可以写四次日期。 但是我不希望查询看起来很大。因此,在一个查询中如何获取数据

I've data of last 4 years that is 2018,2019,2020,2021.
Out of this I need data from JANUARY TO MARCH in all 2018,2019,2020,2021.

I can write date between four times.
But I don't want the query to look very big. So in a single query how to get that data

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

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

发布评论

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

评论(1

吾性傲以野 2025-02-21 01:47:56

如果有一个date datatype列(应该为;我们称其为datum),则可以例如,

select *
from your_table
where extract(year from datum) in (2018, 2019, 2020, 2021)
  and extract(month from datum) in (1, 2, 3)
order by datum;

如果将日期存储为字符串(中varchar2 列),然后首先使用to_date函数使用适当的格式模型将其转换为有效date datatype值。

例如,如果将值存储为20220715(这是yyyymmdd),则

to_date(datum, 'yyyymmdd')

其余查询是相同的:

select *
from your_table
where extract(year from to_date(datum, 'yyyymmdd')) in ...

当心! Oracle不将字符串作为日期处理!该列中可能有无效的值,例如“ 20AC#713”;将to_date应用于它会引起错误(因为这显然不是日期)。或者,如果有“ 20220230”(2月30日),您将再次出现错误。

始终将日期存储到date数据类型列!

If there's a DATE datatype column (should be; let's call it datum), then you could e.g.

select *
from your_table
where extract(year from datum) in (2018, 2019, 2020, 2021)
  and extract(month from datum) in (1, 2, 3)
order by datum;

If you stored dates as strings (into a VARCHAR2 column), then first convert it to a valid date datatype value using the TO_DATE function with appropriate format model.

For example, if values are stored as 20220715 (which is yyyymmdd),

to_date(datum, 'yyyymmdd')

The rest of query is just the same:

select *
from your_table
where extract(year from to_date(datum, 'yyyymmdd')) in ...

Beware! Oracle doesn't handle strings as dates! There might be invalid values in that column, e.g. "20ac#713"; applying to_date to it will raise error (as that's obviously not a date). Or, if there were "20220230" (30th of February), you'll get error again.

Always store dates into date datatype columns!

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