Thursday, November 15, 2012

How to sort date in mssqlserver


PROBLEM

I want to get distinct dates from my dbtable named tblFormno2 in an ascending order.For that i've written the following query but its not working properly.
Column date_submit is declared as datetime
select distinct (convert(nvarchar(100),date_submit,103)) as dob from 
tblFormno2
order by dob asc
Here the the output is shown as
05/07/2011
06/03/2011
06/07/2011
07/04/2011
08/01/2012
instead of
06/03/2011
07/04/2011
05/07/2011
06/07/2011
08/01/2012
How to solve this problem ???


SOLUTION

How about
select convert(nvarchar(10), date_submit_inner, 103) as date_submit from 
(
select distinct date_submit as date_submit_inner from tblFormno2
) as T
order by T.date_submit_inner asc


No comments:

Post a Comment