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 it as string sorting. But i want them to be sorted as date.
I need some help to achieve both in the same query. Kindly help me on the same.
The query which i used was,
SELECT to_char( t1.your_date_column1, your_format_mask ) as alias,
FROM your_table1 t1,your_table2
ORDER BY t1.your_date_column1
It sounds like you want something like
In the
SELECTlist, you want to return a character string that represents the date in your preferred format. In theORDER BYclause, you want to order by the actual date. Using the standardEMPandDEPTtables, for exampleIf you add a DISTINCT, the problem is that Oracle doesn’t know that the function you are applying (in this case TO_CHAR) provides a one-to-one mapping from the data in the table to the data in the output. For example, two different dates (October 1, 2010 10:15:15 and October 1, 2010 23:45:50) might generate the same character output, forcing Oracle to eliminate one of the two ’01-10-2010′ strings but the two dates would sort differently. You can rectify that problem by nesting your query and converting the string back to a date after doing the
DISTINCTand before doing theORDER BY