?
Solved

SQL Sever  extract year and month from datetime

Posted on 2011-02-12
4
Medium Priority
?
695 Views
Last Modified: 2012-05-11
Hi,

How to extract year and month from datetime.
I need it to group rows by month so i need to cut day and time part.

MS SQL Server or better SQL server Compact
0
Comment
Question by:lavitz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Expert Comment

by:BurnieP
ID: 34879985

select year(getdate()), month(getdate())
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 2000 total points
ID: 34880261
convert(char(6),yourdate,112)   as YYYYMM
0
 
LVL 9

Expert Comment

by:xav056
ID: 34881032
Year(getdate())  ,month(getdate())

group by Year(getdate(),Month(getDate())
0
 
LVL 9

Expert Comment

by:sarabhai
ID: 34888132
Sql server provides datetime function for extracting year and month from datetime field.

select year(datetimefield) AS YEAR, month(datetimefiled) AS MONTH FROM TABLE

but here performance may go down,
it is better to do have year and month field with your table having default values  year(datetimefield) and  month(datetimefiled).
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question