2013-10-04 82 views
0
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 

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

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  

它刚刚得到的具体日期,但didm't数天的每个月总数..帮助我..请参阅

+0

请注明的答案,如果你认为它的优点是接受。 –

回答

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

它完全得到每月的天数:)))怎么样,如果你有一个记录,你需要计算你有多少天登录/签到在某一个月? –

+0

但是你可以把你给定的月份或给定的年份放在参数中。尝试使用你的一点点的特定表日期列的代码存储登录或注销时间 – kaushik0033

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) 

注列[daytime_count]仅在需要的领域DateRecord]有次行吟诗人比12:00 AM(即其“剪掉”时间,以便你处理日期在上午12点)

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

见:What do BETWEEN and the devil have in common?

+0

它的工作xD你真是太棒了! –

+0

它完全检索2月和3月的总天数..如果我只需要检索2月的总天数,那么该怎么办? –

+0

对于某个日期范围添加一个where子句,我将编辑答案,以便您可以看到一个示例 –

0

如果你的表称为员工,那么这将这样的伎俩:

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

它得到的总天数..谢谢:)) –

+0

它完全检索2月和3月的总天数..如果我只需要检索2月的总天数呢? –

+0

如果你只想在二月,任何一年,然后像在我的iPad上的'转换(varchar,DateRecord,112)/ 100 - convert(varchar,DateRecord,112)/ 10000 * 100 = 2'抱歉。 –

1

我有改变几个名字hopr你不会介意

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

0

您最初的查询是差不多吧,仅仅局限于N从组中删除DATEPART(dd,Daterecord),它会起作用。添加HAVING子句中找到从二月份的记录:

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 
0

试试这个...

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