95

I need the month+year from the datetime in SQL Server like 'Jan 2008'. I'm grouping the query by month, year. I've searched and found functions like datepart, convert, etc., but none of them seem useful for this. Am I missing something here? Is there a function for this?

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
Malik Daud Ahmad Khokhar
  • 13,470
  • 24
  • 79
  • 81
  • @Evan Carroll - does this constitute hijacking the question? If you have a superior answer to the question, should it not be supplied as an answer to the question instead of pointing people to a different question? – STLDev Apr 04 '17 at 00:25
  • @STLDeveloper how is it hijacking. The question calls for 2005? I didn't put that there. I'm not looking for 2005.. Unfortunately, the best answer here doesn't work on 2005, it's 2012-explicit. Should I downvote the 2005 answers for being archaic? – Evan Carroll Apr 04 '17 at 00:28
  • @STLDeveloper actually, I don't care here. =) my official stance has always been to advise people to use PostgreSQL, not sure why I'm trying to help. – Evan Carroll Apr 04 '17 at 00:31
  • It just seemed odd to me that you went back to create a new version of the question for a different version of the product, and then to answer that question, which I guess is fine. – STLDev Apr 04 '17 at 00:33

20 Answers20

176
select 
datepart(month,getdate()) -- integer (1,2,3...)
,datepart(year,getdate()) -- integer
,datename(month,getdate()) -- string ('September',...)
SWeko
  • 30,434
  • 10
  • 71
  • 106
HS.
  • 15,442
  • 8
  • 42
  • 48
  • So, I think this is what I want, but I do not see how TSQL would understand the difference between `datepart(month,getdate())` and `datepart(year,getdate())`. What are **month** and **year**? –  Jun 03 '13 at 14:30
  • 4
    @jp2code **month** and **year** are basically constants defined here: http://msdn.microsoft.com/en-us/library/ms174420.aspx. TSQL understands them the same way you do, by reading them :) – Zachary Yates Jun 05 '13 at 23:44
  • 1
    LOL - Thanks Zach. I starred at that a long time before figuring it out on a different site. –  Jun 06 '13 at 12:13
  • Much cleaner than convert function. #cleanCode – RBT Jan 09 '17 at 02:50
80

If you mean you want them back as a string, in that format;

SELECT 
  CONVERT(CHAR(4), date_of_birth, 100) + CONVERT(CHAR(4), date_of_birth, 120) 
FROM customers

Here are the other format options

Sampson
  • 265,109
  • 74
  • 539
  • 565
robsoft
  • 5,525
  • 4
  • 35
  • 47
  • I tried using CONVERT(CHAR(4), GetDate(), 100) on my SQL Server and I got "09 1" instead. I got the space and one "1" for the 17. using "select CONVERT(varchar, GetDate(), 100)" yield "09 17 2009 4:59PM" – Nap Sep 17 '09 at 08:00
  • 1
    That's weird - from the docs I linked to, 100 should return a three-letter month abbreviation and a space, if chucked into a CHAR(4). But then, the result you've posted for a CONVERT(varchar, getdate(), 100) looks different to what I'd expect to see. Which version of SQLServer are you using? Which localisation/internationalisation settings are you using (not that this should matter to a 100 format). – robsoft Sep 17 '09 at 08:34
71

Beginning with SQL Server 2012, you can use:

SELECT FORMAT(@date, 'yyyyMM')
CrimsonKing
  • 2,696
  • 1
  • 14
  • 11
13

In SQL server 2012, below can be used

select FORMAT(getdate(), 'MMM yyyy')

This gives exact "Jun 2016"

webbuilder
  • 173
  • 1
  • 9
13

Use:

select datepart(mm,getdate())  --to get month value
select datename(mm,getdate())  --to get name of month
OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
don
  • 131
  • 1
  • 2
12

Funny, I was just playing around writing this same query out in SQL Server and then LINQ.

SELECT 
    DATENAME(mm, article.Created) AS Month, 
    DATENAME(yyyy, article.Created) AS Year, 
    COUNT(*) AS Total 
FROM Articles AS article 
GROUP BY 
    DATENAME(mm, article.Created), 
    DATENAME(yyyy, article.Created) 
ORDER BY Month, Year DESC

It produces the following ouput (example).

Month | Year | Total

January | 2009 | 2
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike Geise
  • 825
  • 7
  • 15
7

How about this?

Select DateName( Month, getDate() ) + ' ' + DateName( Year, getDate() )
Sampson
  • 265,109
  • 74
  • 539
  • 565
GordyII
  • 7,067
  • 16
  • 51
  • 69
6

That format doesn't exist. You need to do a combination of two things,

select convert(varchar(4),getdate(),100)  + convert(varchar(4),year(getdate()))
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SQLMenace
  • 132,095
  • 25
  • 206
  • 225
5

the best way to do that is with :

dateadd(month,datediff(month,0,*your_date*),0)

it will keep your datetime type

cyber cyber1621
  • 167
  • 2
  • 6
  • 1
    This is by far the best answer for forcing any date to be just the month and year, ignoring day and time components. Much better than the accepted answer because the information can be sorted correctly and functions correctly as a date in downstream reporting tools. – Jamie Thomas Dec 01 '16 at 16:12
  • I wholeheartedly agree that this is by far the best way to get the beginning of the month. It keeps the data type datetime, and leaves the rendering up to the front end, where it should be. This is the correct way to do things like group rows by the month of a field. – Jeff Breadner Jun 14 '18 at 22:52
5
( Month(Created) + ',' + Year(Created) ) AS Date
Sampson
  • 265,109
  • 74
  • 539
  • 565
2
cast(cast(sq.QuotaDate as date) as varchar(7))

gives "2006-04" format

Gareth Thomas
  • 420
  • 3
  • 4
2

The question is about SQL Server 2005, many of the answers here are for later version SQL Server.

select convert (varchar(7), getdate(),20)
--Typical output 2015-04

SQL Server 2005 does not have date function which was introduced in SQL Server 2008

TheTechGuy
  • 16,560
  • 16
  • 115
  • 136
2

returns the full month name, -, full year e.g. March-2017

CONCAT(DATENAME(mm, GetDate()), '-', DATEPART(yy, GetDate()))
CDspace
  • 2,639
  • 18
  • 30
  • 36
RobertC
  • 21
  • 1
1

Converting the date to the first of the month allows you to Group By and Order By a single attribute, and it's faster in my experience.

declare @mytable table(mydate datetime)
declare @date datetime
set @date = '19000101'
while @date < getdate() begin
    insert into @mytable values(@date)
    set @date = dateadd(day,1,@date)
end

select count(*) total_records from @mytable

select dateadd(month,datediff(month,0,mydate),0) first_of_the_month, count(*) cnt
from @mytable
group by dateadd(month,datediff(month,0,mydate),0)
Ron Smith
  • 3,241
  • 1
  • 13
  • 16
1
---Lalmuni Demos---
create table Users
(
userid int,date_of_birth date
)
---insert values---
insert into Users values(4,'9/10/1991')

select DATEDIFF(year,date_of_birth, getdate()) - (CASE WHEN (DATEADD(year, DATEDIFF(year,date_of_birth, getdate()),date_of_birth)) > getdate() THEN 1 ELSE 0 END) as Years, 
MONTH(getdate() - (DATEADD(year, DATEDIFF(year, date_of_birth, getdate()), date_of_birth))) - 1 as Months, 
DAY(getdate() - (DATEADD(year, DATEDIFF(year,date_of_birth, getdate()), date_of_birth))) - 1 as Days,
from users
Andrea
  • 11,801
  • 17
  • 65
  • 72
1

I had the same problem and after looking around I found this:

SELECT DATENAME(yyyy, date) AS year
FROM Income
GROUP BY DATENAME(yyyy, date)

It's working great!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Yes, you can use datename(month,intime) to get the month in text.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Alok Kumar
  • 21
  • 1
0
  ,datename(month,(od.SHIP_DATE)) as MONTH_

Answer: MONTH_ January January September October December October September

0

It's work great.

DECLARE @pYear VARCHAR(4)

DECLARE @pMonth VARCHAR(2)

DECLARE @pDay VARCHAR(2)

SET @pYear  = RIGHT(CONVERT(CHAR(10), GETDATE(), 101), 4)

SET @pMonth = LEFT(CONVERT(CHAR(10), GETDATE(), 101), 2)

SET @pDay   = SUBSTRING(CONVERT(CHAR(10), GETDATE(), 101), 4,2)

SELECT @pYear,@pMonth,@pDay
tso
  • 4,732
  • 2
  • 22
  • 32
GanbatSu
  • 1
  • 1
-3

The following works perfectly! I just used it, try it out.

date_format(date,'%Y-%c')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matteo
  • 1