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?
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];
You can use year() function in sql to get the year from the specified date.
select datepart(month,getdate()) -- integer (1,2,3...) ,datepart(year,getdate()) -- integer ,datename(month,getdate()) -- string ('September',...)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With