A1VBCode Forums

how to select distinct month from the database


http://www.a1vbcode.com/vbforums/Topic28057.aspx

By Makura - 8/18/2009

Hi!



I want to select distinct month from the database table. How can i do that anyone can help me.

my database table records are as



Transaction_Date(mm/dd/yyyy)

01/01/2009

01/15/2009

01/25/2009

02/05/2009

02/20/2009

02/29/2009

03/10/2009

03/15/2009

03/28/2009

04/15/2009

04/22/2009

04/27/2009



using where statement less than today(05/01/2009) date



example

month

01

02

03

04




By eralper - 10/6/2009

The problem is if you have records from previous years, then this means you have possibilty to have rows from December,2008.

Then you will have a list beginning from 1 to 12

Perhaps the query might have a starting date criteria or a year criteria for OrderDate

SELECT

DISTINCT MONTH(OrderDate) AS [Month]

FROM Sales.SalesOrderHeader

WHERE OrderDate < GETDATE() and YEAR(OrderDate) = YEAR(GETDATE())

ORDER BY [Month]