Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5300
  • Last Modified:

how to get the Month and Year from date

I want to extract MONTH-YEAR out of the date (datetime format)

Please advice how to do it...

Thanks in advance
0
redgreenred
Asked:
redgreenred
2 Solutions
 
powercramCommented:
In what context?
0
 
chapmandewCommented:
select mont(datefield), year(datefield)
from yourtable
0
 
chapmandewCommented:
whoops...

select month(datefield), year(datefield)
from yourtable
0
 
lluddenCommented:
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
 
cpleong99Commented:

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

this will return Month-Year
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now