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
 question

Author  Topic 

Trininole
Yak Posting Veteran

83 Posts

Posted - 2012-04-09 : 10:22:06
I am trying to pull some data from January 2011 till March 2012 by month. What would the syntax be for that?

Roger DeFour

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-04-09 : 10:30:43
That all depends on what your data looks like. Do you have a field that is a date data type? Also, when you say you want this by month, do you mean that you have, say, records for various days in a month and you need them grouped into just one record for that month?

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-04-09 : 10:31:19


select dateadd(month, dateadiff(month, 0, datecol), 0),
<aggregate function> ( <some column> )
from sometable
where datecol >= '2011-01-01'
and datecol < '2012-04-01'
group by dateadd(month, dateadiff(month, 0, datecol), 0)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Trininole
Yak Posting Veteran

83 Posts

Posted - 2012-04-09 : 10:52:03
I need them grouped into one record by month.

Roger DeFour
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-04-09 : 10:56:47
quote:
Originally posted by Trininole

I need them grouped into one record by month.

Roger DeFour



what do you mean ?

Please provide some sample data and expected result


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Trininole
Yak Posting Veteran

83 Posts

Posted - 2012-04-09 : 11:09:01
well what is the syntax for displaying the month name in sql?

Roger DeFour
Go to Top of Page

Trininole
Yak Posting Veteran

83 Posts

Posted - 2012-04-09 : 11:09:42
For example January, February, March etc.

Roger DeFour
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-04-09 : 11:10:29
quote:
Originally posted by Trininole

well what is the syntax for displaying the month name in sql?

Roger DeFour



datename( month, <datecol> )


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-09 : 11:10:44
quote:
Originally posted by Trininole

For example January, February, March etc.

Roger DeFour


use
DATENAME(mm,yourdatecolumn)

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

Go to Top of Page
   

- Advertisement -