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.
| Author |
Topic |
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-07-30 : 23:55:10
|
| selectSUM(1) as total,MONTH(dateon) as month,categoryfrom tableAwhere dateon between '2012-05-01' and '2012-06-30'group by MONTH(dateon),categoryi get the result as below:total month category20 5 categoryA40 5 categoryB60 6 categoryAhow can i change the month to Name?MonthMayMayJune |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-30 : 23:58:37
|
| [code]selectSUM(1) as total,DATENAME(mm,dateon) as month,categoryfrom tableAwhere dateon between '2012-05-01' and '2012-06-30'group by DATENAME(mm,dateon),category[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-07-31 : 00:10:54
|
| Thanks it worksAnother doubt, im declaring the dateon as below:declare @dateon datetimehow can i declare in month?... where dateon in ('June','July','May') |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2012-07-31 : 09:38:09
|
| If your months are sequential then just trim up your dates to be within the range you're after. You can find examples of this all over. Here's a quick one from Google:http://blog.sqlauthority.com/2007/08/18/sql-server-find-last-day-of-any-month-current-previous-next/replace GetDate() with your parameter.If the months are not sequential you need to look at how you intend to pass the required list in, but you'll most likely be using where DATEPART(mm, dateon) in ..... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-31 : 09:41:36
|
quote: Originally posted by peace Thanks it worksAnother doubt, im declaring the dateon as below:declare @dateon datetimehow can i declare in month?... where dateon in ('June','July','May')
are you planning to pass a single value of month or can it be comma separated list as shown above?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2012-07-31 : 21:52:16
|
| is single value |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-07-31 : 22:50:01
|
| [code]declare @Monthon varchar(20)...where dateon =@Monthon[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|