[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL Sever  extract year and month from datetime

Posted on 2011-02-12
4
Medium Priority
?
704 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

649 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