Question

I'm retrieving data from a SQL Server CE datetime column, I used

CONVERT(NVARCHAR(10), dateHdr, 103)

function to get date from my datetime column but the problem is that it's not ordering column by date properly.

This my query:

SELECT 
   CONVERT(NVARCHAR(10), dateHdr, 103) 
FROM 
   tables0 
GROUP BY 
   CONVERT(NVARCHAR(10), dateHdr, 103) 
ORDER BY 
   CONVERT(NVARCHAR(10), dateHdr, 103) ASC

but it orders columns like this

01/08/2013
01/09/2013
02/08/2013
02/09/2013
Was it helpful?

Solution

That is because you are using VARCHAR(10) - it is expected. You might want to use something like below:

SELECT 
CONVERT(DATE,BusDate,103) as Date 
FROM Table
--GROUP BY CONVERT(DATE,BusDate,103) 
ORDER BY Date ASC
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top