Solved

SQL Sever  extract year and month from datetime

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql Server group by 10 44
ServiceCenter IR Query Expressions 1 40
Text file into sql server 5 32
ORA-02288: invalid OPEN mode 2 57
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
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…

838 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