tags:

views:

41048

answers:

8

Ok, this should be an easy question to answer. 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?

+3  A: 

that format doesn't exists you need to do a combo of two things

select convert(varchar(4),getdate(),100)  + convert(varchar(4),year(getdate()))
SQLMenace
+5  A: 

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

robsoft
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"
Nassign
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
+9  A: 
select 
datepart(month,getdate()) -- integer (1,2,3...)
,datepart(year,getdate()) -- integer
,datename(month,getdate()) -- string ('September',...)
HS
A: 

(Month(Created) + ',' + Year(Created)) AS Date

A: 

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

Produces the following ouput (example).

Month | Year | Total

January | 2009 | 2

bah I just realized someone bumped an old question...

Mike Geise
A: 

Hey... If it's still relevant 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!

A: 

How about this?

Select DateName(Month,getDate()) + ' ' + DateName(Year,getDate())

GordyII
A: 

yes we can use datename(month,intime) to get the month in text

Alok

Alok Kumar