Search code examples
sqloracle-databasedatefor-loopcriteria

Need to get records from db for current date(excluding time)


I have written below query to get the records:

Select * 
  from Scf_Invoice i 
 where cast(i.inv_Acceptance_Date as date) = TO_DATE('2018-12-18', 'YYYY-MM-DD');

But, I get no resultseven if there are 2 records for the given date. I think it's considering time also. When I do

Select * 
  from Scf_Invoice i 
 where cast(i.inv_Acceptance_Date as date) > TO_DATE('2018-12-18', 'YYYY-MM-DD');

I get the results, But I want those results using ' = ' parameter. What change should I do in my query?


Solution

  • Use trunc to get day

      trunc(i.inv_Acceptance_Date) =
    

    The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day.

    Select * 
    from Scf_Invoice i 
    where trunc(i.inv_Acceptance_Date) = TO_DATE('2018-12-18', 'YYYY-MM-DD');