Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact


how to select distinct month from the database


how to select distinct month from the database

Author
Message
Makura
Makura
Forum God
Forum God (4K reputation)Forum God (4K reputation)Forum God (4K reputation)Forum God (4K reputation)Forum God (4K reputation)Forum God (4K reputation)Forum God (4K reputation)Forum God (4K reputation)Forum God (4K reputation)

Group: Forum Members
Posts: 14, Visits: 129
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






Max
Mark
Mark
Forum God
Forum God (141K reputation)

Group: Moderators
Posts: 1.1K, Visits: 11K
Using the Northwinds SQL database as an example, this should work

SELECT DISTINCT MONTH(OrderDate) AS [Month]
FROM         Orders
WHERE     (OrderDate < CONVERT(DATETIME, '1996-09-03 00:00:00', 102))
ORDER BY [Month]

eralper
eralper
Forum God
Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)Forum God (1.2K reputation)

Group: Forum Members
Posts: 4, Visits: 4
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]

 



Eralper
ASP.NET, SQL Server & Windows 7

Edited
10/6/2009 by eralper
GO


Similar Topics


Reading This Topic


Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....

















A1VBCode Forums


Search