I want to convert a dateTime value to Month Year.
I want them like January 2021, March 2021, April 2021.
My current solution works, but only get the first 3 characters of the month
UPDATE TABLE1
SET AccountingPeriod = replace(right(convert(varchar, getdate(), 106), 8), ' ', ' ');
I am getting Oct 2021, but I would like to get October 2021.
CodePudding user response:
You can use DATENAME()
:
SELECT CONCAT(DATENAME(MONTH, GETDATE()), ' ', YEAR(GETDATE()))
CodePudding user response:
I don't think that there is any standard date/datetime formats that will get you this (using CONVERT
).
When there is not standard format available that directly supports your use case, personally, I prefer to use DATENAME
function for this in SQL Server. It is a very easy function that allows you to pull different pieces of the date/datetime for the purpose of display. Using this, you can pull in the discrete pieces you need and then place them together in a single string using CONCAT
.
Something like this would work for your situation:
SELECT
CONCAT(DATENAME(MONTH,GETDATE()),' ',DATENAME(YEAR,GETDATE()))
CodePudding user response: