Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I've got a column with all these different dates, and need to select just the unique month + year.Example of dates: 2007-5-012007-5-132007-5-282007-6-152007-6-152007-8-01and with the query I just want to pull:MAY 2007JUNE 2007AUGUST 2007is 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.
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' ) aorder by 1Results:Start_Of_Month Year Month ------------------------------------------------------ ----------- -------2007-05-01 00:00:00.000 2007 May2007-06-01 00:00:00.000 2007 June2007-07-01 00:00:00.000 2007 July2007-08-01 00:00:00.000 2007 August(4 row(s) affected)[/code]CODO ERGO SUM
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 @TSELECT '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 tGROUP BY YEAR(A), DATENAME(MONTH,A)
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?