13,260,383 members (44,892 online)
Rate this:
See more:
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?
Posted 24-Jan-13 22:48pm

Rate this:

## Solution 1

`Select * from Drug Where Month(getdate())=Month(CreationDate) and Year(Getdate())=Year(CreationDate)`
v2
Varun GJ 25-Jan-13 4:05am

Thank you
Varun GJ 25-Jan-13 4:16am

How do I Get Previous month for the same scenario?
Devang Vaja 25-Jan-13 4:37am

Select * from Drug where case Year(getdate) when 12 then Month(getdate())-1=Month(CreationDate) and Year(getdate())-1=year(creationDate) else month(getdate())-1=month(CreationDate) and year(getdate())=year(creationDate) end
Rate this:

## Solution 2

You need to use
```DATEPART( datepart , date )
```

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())```

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
AND

All the best!
v4
Varun GJ 25-Jan-13 4:06am

Thank You RobBranaghan
RobBranaghan 25-Jan-13 4:38am

You're welcome
Varun GJ 25-Jan-13 4:17am

How do I Get Previous month for the same scenario?
RobBranaghan 25-Jan-13 4:38am

See edited code above

Top Experts
Last 24hrsThis month
 OriginalGriff 150 Peter Leow 90 F-ES Sitecore 90 phil.o 65 CPallini 60
 OriginalGriff 4,374 Karthik Bangalore 2,316 ppolymorphe 1,804 Dave Kreskowiak 1,624 CPallini 1,545