Oracle 中的月日比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24699990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Month and day comparison in Oracle
提问by user3790670
How do you compare month and day together in datetime operations in Oracle?
您如何在 Oracle 的日期时间操作中比较月份和日期?
Assume, currentdate as = 15 Aug 2014 (in datetime format).
假设,当前日期为 = 2014 年 8 月 15 日(日期时间格式)。
I have to compare currentdate (excluding year) with 2 different static dates excluding year, e.g.
我必须将当前日期(不包括年份)与不包括年份的 2 个不同静态日期进行比较,例如
Filter as - 15/Aug between 01/Jul and 20/Dec.
I think for the above to work we need to extract day and month together in datetime format. Please suggest answers. Thanks.
我认为要使上述工作正常运行,我们需要以日期时间格式一起提取日期和月份。请提出答案。谢谢。
回答by Joseph B
You can use the TO_CHAR function to convert the date to text and then compare.
您可以使用 TO_CHAR 函数将日期转换为文本,然后进行比较。
SELECT *
FROM mytable
WHERE TO_CHAR(SYSDATE, 'MMDD') BETWEEN TO_CHAR(start_date, 'MMDD') AND TO_CHAR(end_date, 'MMDD');
The BETWEEN operator is inclusive of the limits. If you do not want to include the limits, you can use:
BETWEEN 运算符包含限制。如果不想包含限制,可以使用:
SELECT *
FROM mytable
WHERE TO_CHAR(SYSDATE, 'MMDD') > TO_CHAR(start_date, 'MMDD')
AND TO_CHAR(SYSDATE, 'MMDD') < TO_CHAR(end_date, 'MMDD');
回答by Thorsten Kettner
Simply use to_char to get month and day (both as two-digit values) concatenated, then use between:
只需使用 to_char 将月份和日期(均为两位数字值)连接起来,然后在两者之间使用:
select *
from mytable
where to_char(mydate,'mmdd') between '0815' and '1220';