views:

116

answers:

3

I make a query that return to me the count of Stored Procedure that created everyday as follow

SELECT  convert(varchar, crdate, 103) as Date,Count(*) as Counter
FROM    sysobjects
WHERE   (xtype = 'p') AND (name NOT LIKE 'dt%')
Group by  convert(varchar, crdate, 103)

and its already work but dates appear in string format that i can't order it such as below

01/03/2010  3
01/04/2008  4
01/05/2010  5
01/11/2008  1
01/12/2008  4
02/03/2008  1
02/03/2010  2
02/04/2008  4
02/05/2010  2
02/11/2008  2
02/11/2009  2
02/12/2008  4
03/01/2010  1
03/02/2010  2
03/03/2010  2
03/04/2008  2
03/04/2010  2
03/05/2008  1
03/05/2010  2

I want to make that in which date is in datetime format that i can make order by successfully, i tried convert(datetime, crdate, 103) but it show Full date

any idea of how to do ?

+1  A: 

To get a sortable date you need year, then month, then date. use format "112".

SELECT  convert(varchar, crdate, 112) as Date,Count(*) as Counter
FROM    sysobjects
WHERE   (xtype = 'p') AND (name NOT LIKE 'dt%')
Group by  convert(varchar, crdate, 112)
order by  Date

which gives this:

Date        Counter
20040711    124
20040713    1
20040725    1
20040726    2
20040803    6

If you want to get the right sort order but a differently formatted date, then you can use a subquery like this.

select CONVERT(varchar, GroupDate, 103) Date, Counter
FROM (
    SELECT  MIN(crdate) as GroupDate,Count(*) as Counter
    FROM    sysobjects
    WHERE   (xtype = 'p') AND (name NOT LIKE 'dt%')
    Group by  convert(varchar, crdate, 112)
) A
order by  GroupDate

which yields

Date        Counter
11/07/2004  124
13/07/2004  1
25/07/2004  1
26/07/2004  2
03/08/2004  6
Sam
thanks, that's what i need
Space Cracker
+1  A: 

How about this:

select dt, COUNT(*) from
(
    SELECT  convert(datetime, convert(varchar, crdate, 112)) as dt
    FROM    sysobjects
    WHERE   (xtype = 'p') AND (name NOT LIKE 'dt%')
) DayOnly
group by dt
order by dt asc
Valentino Vranken
another right answer
Space Cracker
+1  A: 
SELECT DATEADD(day, DATEDIFF(day, 0, o.crdate), 0) [Date],
       COUNT(*) [Counter]
FROM sys.sysobjects o
WHERE o.xtype = 'p' AND o.name NOT LIKE 'dt%'
GROUP BY DATEADD(day, DATEDIFF(day, 0, o.crdate), 0)
Anthony Faull
another right answer
Space Cracker