如何计算MSSQL中给定月份和给定年份的天数

时间:2013-10-04 12:11:37

标签: sql-server sql-server-2005 date

EmployeeID    RecordID         DateRecord
1               1         2/19/2013 12:00:00 AM
1               2         2/21/2013 12:00:00 AM
1               3         2/23/2013 12:00:00 AM
1               4         2/27/2013 12:00:00 AM
1               5         3/3/2013 12:00:00 AM
2               11        3/10/2013 12:00:00 AM
2               12        3/14/2013 12:00:00 AM
1               14        3/16/2013 12:00:00 AM

我如何计算天数?

2013年2月的例子有“19,21,23,27”,应计入“4”天...... ??

我找到了这种方法..

SELECT DATEPART(yy, Daterecord),
   DATEPART(mm, Daterecord),
   DATEPART(dd, Daterecord),
   COUNT(*)

FROM Records

GROUP BY DATEPART(yy, Daterecord),
     DATEPART(mm, Daterecord),
     DATEPART(dd, Daterecord)

并导致......

2013    2   19  1    
2013    2   21  1    
2013    2   23  1    
2013    2   27  1    
2013    3   3   1    
2013    3   10  1    
2013    3   14  1    
2013    3   16  1    

它只是得到具体的日期,但没有计算每个月的总天数..帮帮我..请

6 个答案:

答案 0 :(得分:1)

我改变了一些你不介意的名字。

WITH Emp_CTE AS (

    SELECT EmployeeID ,DATEPART(yy, Daterecord) AS years,
       DATEPART(mm, Daterecord) AS months
      -- DATEPART(dd, Daterecord) AS days


    FROM testTrial
    )
    SELECT COUNT(months) AS noOfMonths ,* FROM Emp_CTE GROUP BY  months,EmployeeID,years

SqlFiddle

答案 1 :(得分:0)

试试这个: -

1:查找我们目前所在月份的天数

DECLARE @dt datetime
SET     @dt = getdate()

SELECT @dt AS [DateTime],
   DAY(DATEADD(mm, DATEDIFF(mm, -1, @dt), -1)) AS [Days in Month]Solution 

2:查找给定月份组合中的天数

DECLARE @y int, @m int
SET     @y = 2012
SET     @m = 2

SELECT @y AS [Year],
   @m AS [Month],
   DATEDIFF(DAY,
            DATEADD(DAY, 0, DATEADD(m, ((@y - 1900) * 12) + @m - 1, 0)),
            DATEADD(DAY, 0, DATEADD(m, ((@y - 1900) * 12) + @m, 0))
       ) AS [Days in Month]

答案 2 :(得分:0)

建议的代码中没有'yearmonth'?

尝试这个

select
           datename(month,daterecord) as [Month]
         , year(DateRecord)           as [Year]
         , count(distinct DateRecord ) as day_count
         , count(distinct dateadd(day, datediff(day,0, DateRecord ), 0)) as daytime_count
from your_table
where ( DateRecord >= '20130201' and DateRecord < '20130301' )
group by
           datename(month,daterecord)
         , year(DateRecord)

请注意,仅当字段[DateRecord]的时间超过12:00 AM时才需要列[daytime_count](即它“减少”次数,因此您处理12:AM的日期)

关于日期范围选择:很多人会觉得使用'between'是解决方案然而这是不正确的,最安全最可靠的方法如上所示。请注意,更高的日期是3月1日,但我们要求的信息少于3月1日,所以我们不需要担心闰年,我们也不必担心小时和分钟。

请参阅:What do BETWEEN and the devil have in common?

答案 3 :(得分:0)

如果您的表名为Employee,那么这将解决问题:

select convert(varchar, DateRecord, 112)/ 100, count(*)
  from Employee
 group by convert(varchar, DateRecord, 112)/ 100

答案 4 :(得分:0)

您的初始查询几乎是正确的,只需从分组中删除DATEPART(dd,Daterecord)即可。添加HAVING子句以查找2月份的记录:

SELECT
    DATEPART(yy, Daterecord),
    DATEPART(mm, Daterecord),   
    COUNT(1)
FROM
    Records
GROUP BY
    DATEPART(yy, Daterecord),
    DATEPART(mm, Daterecord)
HAVING
    DATEPART(yy, eCreationTime) = 2013
AND DATEPART(mm, Daterecord) = 2

答案 5 :(得分:0)

试试这个......

    declare @date2 nvarchar(max)
    set @date2 = (select getdate())
    select DateDiff(Day,@date2,DateAdd(month,1,@date2))