Solved

SQL Sever  extract year and month from datetime

Posted on 2011-02-12
4
685 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate Scripts of Schema/Data with "WHERE" clause 6 62
SQL Server Insert where not exists 24 41
Count with a subquery showing details 10 44
TSQL - How to declare table name 26 30
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…

772 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