我如何 select SQL 每年的日期范围

How do i select the ranges of date every year in SQL

我有一个 10 年的数据库,我想 select 从 2015 年 10 月 1 日到 2016 年 1 月 1 日的日期以及从 2016 年 1 月 1 日到 1- 的日期每年 2016 年 6 月。这是我的代码:

WHERE date > to_date('01-OTT-15') AND date <= to_date('01-GEN-16')
WHERE date > to_date('01-GEN-16') AND date <= to_date('01-GIU-16')
WHERE date > to_date('01-GIU-16') AND date <= to_date('01-OTT-16')
WHERE date > to_date('01-OTT-16') AND date <= to_date('01-GEN-17')
WHERE date > to_date('01-GEN-17') AND date <= to_date('01-GIU-17')

以此类推直到今天

我想知道是否有更精简的代码或循环来复制此代码。

听起来您想排除所有年份的七月、八月和九月(也许还有六月,我无法从问题中分辨出来)。

您可以将它放在一个 where 子句中。使用 SQL 标准语法(也适用于 Oracle):

where extract(month from date) not in (7, 8, 9)

或更积极:

where extract(month from date) in (1, 2, 3, 4, 5, 6, 10, 11, 12)