Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select records for the Current Month In Oracle?

Tags:

java

oracle11g

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:

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.

like image 547
Stanley Mungai Avatar asked Sep 02 '25 11:09

Stanley Mungai


1 Answers

If purchase_date is a DATE.

select * 
from 
   purchases 
where 
   purchase_date >= trunc(sysdate,'mm')

If purchase_date is a 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.

like image 158
Tulains Córdova Avatar answered Sep 04 '25 01:09

Tulains Córdova