java 在 Oracle 中选择当月的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12727602/
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
select records for the Current Month In Oracle
提问by ErrorNotFoundException
Hi Guys I am writing a Java Program where I am required to select Records for the Current Month from an Oracle database. Any time I need to select Records for the Current day I use:
嗨,伙计们,我正在编写一个 Java 程序,我需要从 Oracle 数据库中选择本月的记录。任何时候我需要为我使用的当天选择记录:
select * from purchases where purchase_date = to_char(sysdate, 'DD-MON-YYYY')
What Method should I use to select records from the current Month? On the table the dates appear like:
我应该使用什么方法来选择当月的记录?在表格上,日期显示如下:
10/4/2012 //for 4th October 2012
I tried
我试过
select * from purchases where purchase_date like to_char(sysdate, 'MON-YYYY')
This selects nothing of course please assist.
这当然没有选择,请帮助。
回答by Tulains Córdova
If purchase_date
is a DATE.
如果purchase_date
是日期。
select *
from
purchases
where
purchase_date >= trunc(sysdate,'mm')
If purchase_date
is a VARCHAR
如果purchase_date
是 VARCHAR
select *
from
purchases
where
to_date(purchase_date,'mm/dd/yyyy') >= trunc(sysdate,'mm')
trunc(sysdate,'mm') truncates the current date keeping the month and obviously the year, so 10/04/2012 08:58
is truncated to 10/01/2012 00:00
.
trunc(sysdate,'mm') 截断当前日期,保留月份和显然年份,因此10/04/2012 08:58
被截断为10/01/2012 00:00
.
回答by GriffeyDog
Assuming purchase_date is a VARCHAR2
and formatted like DD-MON-YYYY
, which appears to be the case, then:
假设 purchase_date 是 aVARCHAR2
并且格式为DD-MON-YYYY
,这似乎是这种情况,那么:
select * from purchases where trunc(to_date(purchase_date,'DD-MON-YYYY'),'MONTH') = trunc(sysdate,'MONTH')