Solved

how to get the Month and Year from date

Posted on 2008-06-10
7
5,285 Views
Last Modified: 2012-06-27
I want to extract MONTH-YEAR out of the date (datetime format)

Please advice how to do it...

Thanks in advance
0
Comment
Question by:redgreenred
7 Comments
 
LVL 6

Expert Comment

by:powercram
Comment Utility
In what context?
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
select mont(datefield), year(datefield)
from yourtable
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 25 total points
Comment Utility
whoops...

select month(datefield), year(datefield)
from yourtable
0
 
LVL 18

Expert Comment

by:lludden
Comment Utility
You can also use DATEPART to get any piece of a datetime value:

SELECT DATEPART(datepart,date)
where datepart is:

Datepart Abbreviations 

year     yy, yyyy 

quarter  qq, q 

month    mm, m 

dayofyear dy, y 

day      dd, d 

week     wk, ww 

weekday  dw 

hour     hh 

minute   mi, n 

second   ss, s 

millisecond ms 

Open in new window

0
 
LVL 3

Assisted Solution

by:cpleong99
cpleong99 earned 25 total points
Comment Utility

select cast(month(datefield) as varchar(2)) + '-' + cast(year(datefield) as varchar(4))
from table1

this will return Month-Year
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

728 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

14 Experts available now in Live!

Get 1:1 Help Now