Solved

how to get the Month and Year from date

Posted on 2008-06-10
7
5,290 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
ID: 21755122
In what context?
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 21755133
select mont(datefield), year(datefield)
from yourtable
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 25 total points
ID: 21755135
whoops...

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

Expert Comment

by:lludden
ID: 21756130
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
ID: 21756483

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

this will return Month-Year
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migration MS SQL database to Oracle 30 62
SQL Server 2012 r2 - Varible Table 3 27
Find results from sql within a time span 11 33
transaction in asp.net, sql server 6 33
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

821 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