Solved

SQL Sever  extract year and month from datetime

Posted on 2011-02-12
4
684 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
4 Comments
 
LVL 16

Expert Comment

by:BurnieP
ID: 34879985

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

Accepted Solution

by:
Lowfatspread earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now