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
 General SQL Server Forums
 New to SQL Server Programming
 month

Author  Topic 

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-07-30 : 23:55:10
select
SUM(1) as total
,MONTH(dateon) as month
,category
from tableA
where dateon between '2012-05-01' and '2012-06-30'
group by MONTH(dateon),category

i get the result as below:
total month category
20 5 categoryA
40 5 categoryB
60 6 categoryA

how can i change the month to Name?

Month
May
May
June

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-30 : 23:58:37
[code]
select
SUM(1) as total
,DATENAME(mm,dateon) as month
,category
from tableA
where dateon between '2012-05-01' and '2012-06-30'
group by DATENAME(mm,dateon),category
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-07-31 : 00:10:54
Thanks it works

Another doubt, im declaring the dateon as below:

declare @dateon datetime

how can i declare in month?

... where dateon in ('June','July','May')
Go to Top of Page

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 .....
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-07-31 : 09:41:36
quote:
Originally posted by peace

Thanks it works

Another doubt, im declaring the dateon as below:

declare @dateon datetime

how 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 MVP
http://visakhm.blogspot.com/

Go to Top of Page

peace
Constraint Violating Yak Guru

420 Posts

Posted - 2012-07-31 : 21:52:16
is single value
Go to Top of Page

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 MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -