Home > Software engineering >  I need help understanding why am I am not getting any results with below query
I need help understanding why am I am not getting any results with below query

Time:06-29

Using query #1 below I get the the following results

select DATE, count (DATE) from TABLE1
group by DATE
DATE COUNT(DATE)
6/6/2022 6856
6/6/2022 2:06:10 PM 78895
6/6/2022 2:06:11 PM 90230
6/6/2022 2:06:12 PM 95693
6/6/2022 2:06:13 PM 94352
6/6/2022 2:06:14 PM 9101
6/27/2022 6854
6/27/2022 7:36:58 PM 6422

Using the above results, I am now trying to write a query which will only isolate the 6/27/2022 dates, which are the two line items at the bottom of my results. I've tried using a to_char function as well as Like function but all variations of the queries that I write result in "No Records". In other words, the query runs but I get no results.

Select * from TABLE where to_char(DATE) like '/27/2022%'

Can someone help me with this.

Thank you,

CodePudding user response:

If your column is a DATE data type and you want all the values from one day then:

Select *
from   table_name
where  date_column >= DATE '2022-06-28'
and    date_column <  DATE '2022-06-29'

If you want all the values at a particular instant then:

Select *
from   table_name
where  date_column = DATE '2022-06-28'   INTERVAL '14:06:10' HOUR TO SECOND

or

Select *
from   table_name
where  date_column = TIMESTAMP '2022-06-28 14:06:10';

or

Select *
from   table_name
where  TO_CHAR(date_column, 'YYYY-MM-DD HH24:MI:SS') = '2022-06-28 14:06:10';

If your column is a string data type then use the TO_DATE function to convert it to a date data type and then use one of the queries above.

For example:

Select *
from   table_name
where  TO_DATE(string_column, 'MM/DD/YYYY HH12:MI:SS AM') >= DATE '2022-06-28'
and    TO_DATE(string_column, 'MM/DD/YYYY HH12:MI:SS AM') <  DATE '2022-06-29'
  • Related