如何编写查询以仅获取当前月份详细信息? [英] How to write a Query to Get only current month Details ?

查看:70
本文介绍了如何编写查询以仅获取当前月份详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库表如下..



PKDrugId - Int

DrugName - Varchar(50)

DrugType - Varchar(25)

CreationDate - 日期







我正在尝试生成一份报告,其中列出了当月输入的所有药品名称。我应该如何在SQL Server 2008中为此编写查询?

I have a database table as follows..

PKDrugId - Int
DrugName - Varchar(50)
DrugType - Varchar(25)
CreationDate - Date



I am trying to generate a report which gives all the Drug Names which are entered in the current month. How should i write a query for this in SQL Server 2008?

推荐答案

Select * from Drug Where Month(getdate())=Month(CreationDate) and Year(Getdate())=Year(CreationDate)


您需要使用

You need to use
DATEPART( datepart , date )





有关这方面的一些有用信息,请访问 http://msdn.microsoft.com /en-us/library/ms174420.aspx [ ^ ]



我不确定表名,所以我使用了tblDrugs。





Some useful info on this is available at http://msdn.microsoft.com/en-us/library/ms174420.aspx[^]

I wasn''t sure of the table name, so I used tblDrugs.

SELECT
   DrugName
FROM
   tblDrugs
WHERE
   DATEPART(month,CreationDate) = DATEPART(month, GETDATE())
   AND
   DATEPART(year,CreationDate) = DATEPART(year, GETDATE())





希望能帮助您了解如何使用它!



~~~编辑~~~

如果你想获得上个月的数据,你可以从月中减去1。





Hope that helps you understand how to use it!

~~~Edit~~~
If you wanted to get last months data you could just subtract 1 from the month.

SELECT
   DrugName
FROM
   tblDrugs
WHERE
   DATEPART(month,CreationDate) = DATEPART(month,DATEADD(MONTH, -1,GETDATE()) )
   AND
   DATEPART(year,CreationDate) = DATEPART(year, DATEADD(MONTH, -1,GETDATE()))









一切顺利!





All the best!


这篇关于如何编写查询以仅获取当前月份详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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