且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何编写查询以仅获取当前月份详细信息?

更新时间:2022-12-07 11:50:15

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!