I have a table with the following fields in a SQL Server 2005 database:
id, integervalue, stringcreate_date, datetime
New data is constantly inserted into this table (tens of thousands of records per day), so I use the following query to compare how much data was inserted on different days.
SELECT CONVERT(varchar(10), create_date, 101) as 'Date', COUNT(*) as 'Record Count', FROM the_table GROUP BY CONVERT(varchar(10), create_date, 101) ORDER BY 'Date' desc
This query returns data that looks like this:
12/20/2012 | 48155 12/19/2012 | 87561 12/18/2012 | 71467
However, when I ran this query today, I noticed that sorting did not work as expected with the multi-year cost of data in the database. Instead of the data for this year at the very top of the result set, it ended up below (entries omitted for clarity)
06/29/2012 | 9987 01/04/2013 | 15768 01/03/2013 | 77586 01/02/2013 | 23566
I understand why this is happening, since my formatted date is just a string, and you cannot expect the sql server to sort it like anything except a string. But I would like the order to be accurate. How can i achieve this? (the last day always appears first)
source share