Search code examples
sqloracle-databasedate-formatdate-comparison

How to put where condition in sql for fetching records from a column that has date and time in UTC format?


I have some data in one of the column lastmodified that is in UTC format. Sample data below.

lastmodified column (sample data):

05-NOV-15 08.44.00.682392000 PM
05-NOV-15 08.43.00.808086000 PM
06-NOV-15 06.15.01.035835000 PM
06-NOV-15 06.14.00.694774000 PM
07-NOV-15 04.26.00.851745000 PM
07-NOV-15 04.26.00.690437000 PM
08-NOV-15 04.25.00.850166000 PM
08-NOV-15 04.25.00.683356000 PM
09-NOV-15 04.24.00.821617000 PM
09-NOV-15 04.24.00.768993000 PM
10-NOV-15 04.23.00.804560000 PM
10-NOV-15 01.20.00.501400000 PM

Query:

select *
  from my_table
 where trunc(lastmodified) = to_char(sysdate, 'dd-mon-yy');

The above query works, but does not provide me what I want. How can I get all the records above 10-NOV-15 01.00.00 date-time?


Solution

  • Given that the datatype of lastmodified is TIMESTAMP you should be able to do

    SELECT *
      FROM MY_TABLE
      WHERE TRUNC(LASTMODIFIED) = TRUNC(SYSDATE)
    

    Or, if you really want to get records >= 10-NOV-15 01.00.00 you could use

    SELECT *
      FROM MY_TABLE
      WHERE LASTMODIFIED >= TO_DATE('10-NOV-15 01.00.00', 'DD-MON-RR HH24.MI.SS')
    

    Best of luck.