Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get month and year from a datetime in SQL Server 2005

Tags:

sql

sql-server

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?

like image 687
Malik Daud Ahmad Khokhar Avatar asked Sep 05 '08 11:09

Malik Daud Ahmad Khokhar


People also ask

How do I get the month name and year in SQL?

T-SQL queries to display the year and day numbersGETDATE() AS [CurrentDate]; SELECT DATENAME(DAY, GETDATE() ) AS [Day], GETDATE() AS [CurrentDate]; SELECT DATENAME(YEAR, GETDATE() ) AS [Year], GETDATE() AS [CurrentDate]; SELECT DATENAME(DAY, GETDATE() ) AS [Day], GETDATE() AS [CurrentDate];

How do I select year from datetime in SQL?

You can use year() function in sql to get the year from the specified date.


2 Answers

select  datepart(month,getdate()) -- integer (1,2,3...) ,datepart(year,getdate()) -- integer ,datename(month,getdate()) -- string ('September',...) 
like image 91
HS. Avatar answered Sep 27 '22 17:09

HS.


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

like image 45
robsoft Avatar answered Sep 27 '22 19:09

robsoft