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
2008-09-05 11:08:54
+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
robsoft
2008-09-05 11:12:32
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
2009-09-17 08:00:30
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
2009-09-17 08:34:28
+9
A:
select
datepart(month,getdate()) -- integer (1,2,3...)
,datepart(year,getdate()) -- integer
,datename(month,getdate()) -- string ('September',...)
HS
2008-09-05 12:00:45
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
2009-01-15 11:19:51
A:
How about this?
Select DateName(Month,getDate()) + ' ' + DateName(Year,getDate())
GordyII
2009-05-14 04:40:54