2010-05-19 37 views
1

我作出這樣的回報,我認爲每天創建的存儲過程的計數查詢如下
每天創建多少個存儲過程(轉換日期時間的問題)?

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) 

和已經工作,但日期出現在字符串格式,我不能爲了它,如下面

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 

我想要在這日期是日期時間格式,我可以讓order by成功,我想convert(datetime, crdate, 103)但它顯示出充分的日期

有什麼想法怎麼辦?

+0

空間餅乾,你應該讓包括一些關於轉換日期編輯這個問題的標題。 – DJTripleThreat 2010-05-19 06:12:11

+0

好的,我現在做.. thnx – 2010-05-19 06:43:02

回答

1

要獲得可排序的日期,您需要年份,然後是月份,然後是日期。使用格式「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 

其中給出了這樣的:

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

如果你想找到正確的排序順序,但不同格式的日期,那麼你可以使用子查詢這個樣子。

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 

這將產生

Date  Counter 
11/07/2004 124 
13/07/2004 1 
25/07/2004 1 
26/07/2004 2 
03/08/2004 6 
+0

謝謝,這就是我需要 – 2010-05-19 07:13:22

1

如何:

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 
+0

另一個正確的答案 – 2010-05-19 07:13:46

1
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) 
+1

另一個正確的答案 – 2010-05-19 07:14:05