How to format and sort a date in Oracle?

后端 未结 7 1855
长情又很酷
长情又很酷 2021-02-19 09:50

In my application am trying to format and sort the date, i am using to_char() function to format the date to my required format, but when i sort them it is sorting

7条回答
  •  长发绾君心
    2021-02-19 10:28

    I wanted to Group By and Order By the Date field but the Date field included the Time and I didn't want to include the Time in the grouping and sorting. So I converted the Date to Character and then converted the character back to Date to eliminate the Time and sort by date not by text. That grouped the data by the date and sorted by the date.

    -- Projects initiated by Day.
    select to_date(to_char(psd.PROJECTSTATUSDATE, 'mm/dd/yyyy'),'mm/dd/yyyy') as "Date", count(*)
    from project pj, project_status_date psd
    where PJ.PROJECTTOKEN = PSD.PROJECTTOKEN
          and psd.PROJECTSTATUSDATE > '01-JAN-2001'
          and PSD.PROJECTSTATUSCODE = 'BL'
    group by to_date(to_char(psd.PROJECTSTATUSDATE, 'mm/dd/yyyy'),'mm/dd/yyyy') 
    order by to_date(to_char(psd.PROJECTSTATUSDATE, 'mm/dd/yyyy'),'mm/dd/yyyy') 
    
    Date      Count
    8/16/2013   102
    8/19/2013   77
    8/20/2013   257
    8/21/2013   30
    8/22/2013   173
    8/23/2013   125
    8/26/2013   41
    8/27/2013   25
    8/28/2013   14
    

提交回复
热议问题