如何计算SQL中特定月份的记录数 [英] How to count no of record from a particular month in SQL

查看:86
本文介绍了如何计算SQL中特定月份的记录数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此查询计算数据库中的所有记录存储..i只需要获取当月的记录...



我尝试过:



this query count all record store in database ..i need to get only record of current month ...

What I have tried:

<pre>select  count(*) as present from attendance  where present='P'   and name='dummmy' and [date]>= convert(varchar(11),(DATEADD(mm, DATEDIFF(mm, 0, GETDATE()), 0)),105) and   [date] <=  convert(varchar(11), getdate(), 105)

推荐答案

问题是你正在使用什么服务器?

有一个SQL Server 2015及更高版本中的月份函数:



The question is what server you are using?
There is a month function in SQL Server 2015 and later:

<pre lang="SQL">Select Month(getdate())





这很有可能解决你的问题。



Most likely this should solve your problem.


好的,在这种情况下试试datepart()函数从日期时间中提取月份部分:

OK, in that case try the datepart() function extracting the month portion from a datetime:
DATEPART(month, getdate())





DATEPART(Transact-SQL)| Microsoft Docs [ ^ ]


你可以试试这个:



并确保你也加上一年。否则如果您在表中有3年的记录,它可能会最终为您提供所提供月份的最近3年的数量。



you may try this:

And make sure you add year as well. else if you have records for 3 years in table it might end up providing you count for last 3 years for the provided month.

select  count(*) as present from attendance  where present='P'   and name='dummmy' and
 DATEPART(mm, [date]) = DATEPART(mm, getdate())
AND DATEPART(yy, [date]) = DATEPART(yy, getdate())


这篇关于如何计算SQL中特定月份的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆