Oracle SQL comparison of DATEs returns wrong result

后端 未结 2 1480
青春惊慌失措
青春惊慌失措 2020-12-06 22:52

I have REPORTDATE column in database (DATETIME) type. I want to extract only DATE value from the DATETIME, then to do COUNT

2条回答
  •  难免孤独
    2020-12-06 23:35

    The condition to_char(REPORTDATE, 'DD.MM.YYYY')>'09.11.2013' compare to strings, so 30.10.2013 is after 09.11.2013. You need to compare the dates, not the string values, so you have to change your query to the following.

    SELECT to_char(REPORTDATE, 'DD.MM.YYYY') AS MY, COUNT(1) 
    from INCIDENT
    where trunc(REPORTDATE)> to_date('09.11.2013', 'DD.MM.YYYY')
    GROUP BY to_char(REPORTDATE, 'DD.MM.YYYY')
    

    Note: I added a little modification from count(*) to count(1) for optimize the query having the same results.

提交回复
热议问题