Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 selecting unique month and year

Author  Topic 

sharc
Starting Member

8 Posts

Posted - 2007-07-20 : 15:19:29
I've got a column with all these different dates, and need to select just the unique month + year.

Example of dates:
2007-5-01
2007-5-13
2007-5-28
2007-6-15
2007-6-15
2007-8-01

and with the query I just want to pull:
MAY 2007
JUNE 2007
AUGUST 2007

is this possible :( ? And can anyone point me in the right direction as to how I would go about this?

rmiao
Master Smack Fu Yak Hacker

7266 Posts

Posted - 2007-07-20 : 15:35:59
You can try convert that with substring function and case statement.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2007-07-20 : 15:41:41
[code]


select distinct
Start_Of_Month =dateadd(month,datediff(month,0,MyDate),0),
Year = Year(Mydate),
Month = datename(month,Mydate)
from
(
select MyDate = getdate() union all
select '2007-5-01' union all
select '2007-5-13' union all
select '2007-5-28' union all
select '2007-6-15' union all
select '2007-6-15' union all
select '2007-8-01'
) a
order by 1


Results:
Start_Of_Month Year Month
------------------------------------------------------ ----------- -------
2007-05-01 00:00:00.000 2007 May
2007-06-01 00:00:00.000 2007 June
2007-07-01 00:00:00.000 2007 July
2007-08-01 00:00:00.000 2007 August

(4 row(s) affected)


[/code]

CODO ERGO SUM
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-07-20 : 15:45:37
Here is a sample that can get you started:
DECLARE @T TABLE (A DATETIME)

INSERT @T
SELECT '2007-5-01'
UNION ALL SELECT '2007-5-13'
UNION ALL SELECT '2007-5-28'
UNION ALL SELECT '2007-6-15'
UNION ALL SELECT '2007-6-15'
UNION ALL SELECT '2007-8-01'

SELECT
DATENAME(MONTH,A),
YEAR(A),
COUNT(*)
FROM
@T t
GROUP BY
YEAR(A),
DATENAME(MONTH,A)
Go to Top of Page

sharc
Starting Member

8 Posts

Posted - 2007-07-20 : 15:48:07
wow thanks a lot! and I'm supposing I can use a concat function to just group the month and year as 1 field, right?
Go to Top of Page
   

- Advertisement -